find integers in f.readline()
Tommy Grav
tgrav at me.com
Thu Apr 29 22:34:06 EDT 2010
On Apr 29, 2010, at 10:22 PM, elsa wrote:
> Hi people,
>
> I'm having a problem getting the info I need out of a file.
>
> I've opened the file with f=open('myFile','r').
>
> Next, I take out the first line with line=f.readline()
>
> line looks like this:
>
> '83927 300023_25_5_09_FL 9086 9134 F3LQ2BE01AQLXF 1 49 + 80
> ZA8Z89HIB7M'
>
> I then split it into parts with parts = line.split()
>
> ['83927', '300023_25_5_09_FL', '9086', '9134', 'F3LQ2BE01AQLXF', '1',
> '49', '+', '80', 'ZA8Z89HIB7M']
>
> Now, I need to test whether I can call int(parts[0]) or not. Some of
> the lines in my file start with a value which represents and integer
> (as above), while others are just strings of characters. I want to
> extract just the lines like the one above, that start with an integer.
> Any suggestions?
Take a look at try and except. Using int(parts[0]) will raise an exception
when it fails. Catch the exception and only save the lines when the try
does not fail.
Tommy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100429/39923e2a/attachment-0001.html>
More information about the Python-list
mailing list