find integers in f.readline()

E

elsa

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?

Thanks,

Elsa.
 
A

Alf P. Steinbach

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?

<code>
#Py3

lines = (
"83927 300023_25_5_09_FL 9086 9134 F3LQ2BE01AQLXF 1 49 + 80 ZA8Z89HIB7M",
"blah blah",
"2 small tortoises"
)

for line in lines:
parts = line.split()
if len( parts ) > 0:
try:
v = int( parts[0] )
print( "OK " + line )
except ValueError:
print( "! " + line )
</code>


Cheers & hth.,

- Alf
 
M

MRAB

elsa said:
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?
You can test whether a string contains only digits with the .isdigit()
method:
False

but usually it's better just to catch the ValueError exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top