2

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.

I am finding trouble in appending the list. From my below code, it is just appending the first(0) index of the line. Please help me. Thank you.

import re
hand = open ('a.txt')
lst = list()
for line in hand:
    line = line.rstrip()
    stuff = re.findall('[0-9]+', line) 
    if len(stuff)!= 1  : continue
    num = int (stuff[0])
    lst.append(num)
print sum(lst)
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Bhanu Prakash
  • 49
  • 1
  • 5

2 Answers2

0

Great, thank you for including the whole txt file! Your main problem was in the if len(stuff)... line which was skipping if stuff had zero things in it and when it had 2,3 and so on. You were only keeping stuff lists of length 1. I put comments in the code but please ask any questions if something is unclear.

import re
hand = open ('a.txt')
str_num_lst = list()
for line in hand:
    line = line.rstrip()
    stuff = re.findall('[0-9]+', line)
    #If we didn't find anything on this line then continue
    if len(stuff) == 0: continue
    #if len(stuff)!= 1: continue #<-- This line was wrong as it skip lists with more than 1 element

    #If we did find something, stuff will be a list of string:
    #(i.e. stuff = ['9607', '4292', '4498'] or stuff = ['4563'])
    #For now lets just add this list onto our str_num_list
    #without worrying about converting to int.
    #We use '+=' instead of 'append' since both stuff and str_num_lst are lists
    str_num_lst += stuff

#Print out the str_num_list to check if everything's ok
print str_num_lst

#Get an overall sum by looping over the string numbers in the str_num_lst
#Can convert to int inside the loop
overall_sum = 0
for str_num in str_num_lst:
    overall_sum += int(str_num)

#Print sum
print 'Overall sum is:'
print overall_sum

EDIT:

You are right, reading in the entire file as one line is a good solution, and it's not difficult to do. Check out this post. Here is what the code could look like.

import re

hand = open('a.txt')
all_lines = hand.read() #Reads in all lines as one long string
all_str_nums_as_one_line = re.findall('[0-9]+',all_lines)
hand.close() #<-- can close the file now since we've read it in

#Go through all the matches to get a total
tot = 0
for str_num in all_str_nums_as_one_line:
    tot += int(str_num)

print('Overall sum is:',tot) #editing to add ()
mitoRibo
  • 4,468
  • 1
  • 13
  • 22
  • Thank you so much. I knew I was doing wrong at if len(stuff)... line. I was not able figure it out the aproach. '+=' is the right option. Thanks for sharing that. And as an entry level programmer, I am wondering if can we read() the whole file as a single string and use '[0-9]+' from the string to extract? – Bhanu Prakash Oct 19 '16 at 19:51
  • Yes, good point! I've edited my answer to include that option – mitoRibo Oct 19 '16 at 20:22
  • That;s great. Thank you so much. – Bhanu Prakash Oct 21 '16 at 20:32
0
import re
ls=[];
text=open('C:/Users/pvkpu/Desktop/py4e/file1.txt');
for line in text:
    line=line.rstrip();
    l=re.findall('[0-9]+',line);
    if len(l)==0:
        continue
    ls+=l
for i in range(len(ls)):
    ls[i]=int(ls[i]);
print(sum(ls));