find integers in f.readline()

Discussion in 'Python' started by elsa, Apr 30, 2010.

  1. elsa

    elsa Guest

    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.
     
    elsa, Apr 30, 2010
    #1
    1. Advertising

  2. On 30.04.2010 04:22, * 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?


    <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
     
    Alf P. Steinbach, Apr 30, 2010
    #2
    1. Advertising

  3. elsa

    MRAB Guest

    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?
    >

    You can test whether a string contains only digits with the .isdigit()
    method:

    >>> '83927'.isdigit()

    True
    >>> '-10'.isdigit()

    False
    >>> 'something_else'.isdigit()

    False

    but usually it's better just to catch the ValueError exception.
     
    MRAB, Apr 30, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    5,571
    Andrea Griffini
    Feb 26, 2006
  2. Jeremy

    find the smallest integers

    Jeremy, Feb 8, 2007, in forum: Java
    Replies:
    0
    Views:
    366
    Jeremy
    Feb 8, 2007
  3. gavino
    Replies:
    4
    Views:
    541
    gavino
    Sep 20, 2010
  4. Jean-Michel
    Replies:
    0
    Views:
    362
    Jean-Michel
    Dec 22, 2007
  5. Andrew DeFaria
    Replies:
    1
    Views:
    212
    Ben Morrow
    Jan 30, 2008
Loading...

Share This Page