python - Identify and extract number from text files -


i trying extract data text file. data in file random , has numbers followed code. (example 1.25crow, 4.25crd, 10.25cr) want extract number associated #.##cr index. if find 4.25cr need parse 4.25 , add totals of these numbers. have been able identify lines contain ###.##cr shown below. trying parse number associated cr , put each occurrence in list add together, identify, etc. have looked string.operands , re.match can't come solution. appreciated.

open("some.txt").read() #txt ='cr'  count = 0 line in open("some.txt"):      if 'crd' in line:         pass     elif 'crow' in line:             pass     elif 'cr' in line:         print line         count = count + 1         print  print "total # count "  print count  

yep on write track. need modify last elif block as

elif 'cr' in line:     print line     count = count + 1     value = float(line.split('cr')[0])     listofcrs.append(value) 

have list before if clause defined as

listofcrs = [] 

finally print sum outside loop in

print sum(listofcrs) 

few points

  • use with open('some.txt') f: open file explicitly close file.

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -