Home > Net >  Is there something wrong with my code about regular expressions?
Is there something wrong with my code about regular expressions?

Time:12-24

This is the background on the assignment: In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers. The file contains much of the text from the introduction of the textbook except that random numbers are inserted throughout the text. Here is a sample of the output you might see: The basic outline of this problem is to read the file, look for integers using the re.findall(), looking for a regular expression of '[0-9] ' and then converting the extracted strings to integers and summing up the integers.

This is the file I'm using - https://py4e-data.dr-chuck.net/regex_sum_1705010.txt

I tried to run the following code, but my output was 0. This code is from another forum on this website, but I somewhat tailored it to my needs. import re

>>> hand = open('regex_sum.txt')
>>> num = 0
>>> for line in hand:
...     line = line.rstrip()
...     num = num   sum(map(lambda x: int(x), re.findall('([0-9] )', line)))
print (num)
According to the assignment, I was supposed to get an integer ending with '299'.`

CodePudding user response:

Running the code that you provided works. Make sure that you import the re module, and that you provide the correct file name. Import re by adding the following line:

import re

Please keep the regex_sum_1705010.txt file in the same folder as your python file and use:

hand = open("regex_sum_1705010.txt")

Or provided full path for the file like:

hand = open(r"C:\Users\xyz\Desktop\Programming\regex_sum_1705010.txt")

Note the r before the string.

The complete code would look like:

import re

hand = open("regex_sum_1705010.txt")
num = 0

for line in hand:
    line = line.rstrip()
    num = num   sum(map(lambda x: int(x), re.findall('([0-9] )', line)))
print (num)
output: 519299
  • Related