How to manipulate a contents of file as record structures

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Jan 31 11:49:25 CET 2009


En Sat, 31 Jan 2009 08:35:44 -0200, CK Raju <ck.thrissur at gmail.com>  
escribió:

> I have a text file containing the following alphanumerals.
> aaaaaa22bbbbbbbb55
> hhhhhh11dpdpdpdp22
> kkkkkkk21lokolkolko33
> .....
>
> I need to read the contents as single line, one after the other
> and append the sum of digits at the end.
> aaaaaa22bbbbbbbb5577
> hhhhhh11dpdpdpdp2233
> kkkkkkk21lokolkolko3354
> ....
>
> What would be a simple way to achieve this ?


Suppose f is the open file. To read it a line at a time:
for line in f:
   do_something_with(line)

To search for a number, try the re module:  
http://docs.python.org/library/re.html

To do some calculation with those numbers, remember to convert them to  
integers first:
x="12"
y="34"
x+y gives "1234"
int(x)+int(y) gives 46

Good luck!

-- 
Gabriel Genellina




More information about the Python-list mailing list