extracting numbers from a file, excluding fixed words

Discussion in 'Python' started by dawenliu, Oct 29, 2005.

  1. dawenliu

    dawenliu Guest

    Hi, I have a file with this content:
    xxxxxxx xxxxxxxxxx xxxxx xxxxxxx
    1
    0
    0
    0
    1
    1
    0
    (many more 1's and 0's to follow)
    yyyyy yyyyyy yyy yyyyyy yyyyy yyy

    The x's and y's are FIXED and known words which I will ignore, such as
    "This is the start of the file" and "This is the end of the file". The
    digits 1 and 0 have UNKNOWN length. I want to extract the digits and
    store them in a file. Any suggestions will be appreciated.
     
    dawenliu, Oct 29, 2005
    #1
    1. Advertisements

  2. dawenliu wrote:
    > Hi, I have a file with this content:
    > xxxxxxx xxxxxxxxxx xxxxx xxxxxxx
    > 1
    > 0
    > 0
    > 0
    > 1
    > 1
    > 0
    > (many more 1's and 0's to follow)
    > yyyyy yyyyyy yyy yyyyyy yyyyy yyy
    >
    > The x's and y's are FIXED and known words which I will ignore, such as
    > "This is the start of the file" and "This is the end of the file". The
    > digits 1 and 0 have UNKNOWN length. I want to extract the digits and
    > store them in a file. Any suggestions will be appreciated.
    >


    Open the file and read each line 1 at a time. If the line doesn't equal
    x or y then add the line to a list

    f = open("file.txt")

    numbers = []

    for eachline in f.xreadlines():
    if (eachline <> x) or (eachline <> y):
    numbers.append(eachline)
     
    Daniel Bowett, Oct 29, 2005
    #2
    1. Advertisements

  3. dawenliu

    Kent Johnson Guest

    dawenliu wrote:
    > Hi, I have a file with this content:
    > xxxxxxx xxxxxxxxxx xxxxx xxxxxxx
    > 1
    > 0
    > 0
    > 0
    > 1
    > 1
    > 0
    > (many more 1's and 0's to follow)
    > yyyyy yyyyyy yyy yyyyyy yyyyy yyy
    >
    > The x's and y's are FIXED and known words which I will ignore, such as
    > "This is the start of the file" and "This is the end of the file". The
    > digits 1 and 0 have UNKNOWN length. I want to extract the digits and
    > store them in a file. Any suggestions will be appreciated.
    >


    Off the top of my head (not tested):

    inf = open('input.txt')
    out = open('output.txt', 'w')

    skips = [
    'xxxxxxx xxxxxxxxxx xxxxx xxxxxxx',
    'yyyyy yyyyyy yyy yyyyyy yyyyy yyy',
    ]

    for line in inf:
    for skip in skips:
    if skip in line:
    continue
    out.write(line)

    inf.close()
    out.close()

    Kent
     
    Kent Johnson, Oct 29, 2005
    #3
  4. dawenliu

    Guest

    Thanks Kent. The code looks reasonable, but the result is that, the
    output file comes out identical as the input file, with all the xxxx
    and yyyy remaining inside.
     
    , Oct 29, 2005
    #4
  5. dawenliu

    Guest

    I've changed the code a little bit and works fine now:
    inf = open('input.txt')
    out = open('output.txt', 'w')

    skips = [
    'xxxxxxx xxxxxxxxxx xxxxx xxxxxxx',
    'yyyyy yyyyyy yyy yyyyyy yyyyy yyy']

    for line in inf:
    flag = 0
    for skip in skips:
    if skip in line:
    flag = 1
    continue
    if flag == 0:
    out.write(line)

    inf.close()
    out.close()
     
    , Oct 29, 2005
    #5
  6. dawenliu <> wrote:

    > Hi, I have a file with this content:
    > xxxxxxx xxxxxxxxxx xxxxx xxxxxxx
    > 1
    > 0
    > 0
    > 0
    > 1
    > 1
    > 0
    > (many more 1's and 0's to follow)
    > yyyyy yyyyyy yyy yyyyyy yyyyy yyy
    >
    > The x's and y's are FIXED and known words which I will ignore, such as
    > "This is the start of the file" and "This is the end of the file". The
    > digits 1 and 0 have UNKNOWN length. I want to extract the digits and
    > store them in a file. Any suggestions will be appreciated.


    [[warning, untested code...]]

    infile = open('infile.txt')
    oufile = open('oufile.txt', 'w')
    for line in infile:
    if line.strip().isdigit(): oufile.write(line)
    oufile.close()
    infile.close()


    Alex
     
    Alex Martelli, Oct 29, 2005
    #6
    1. Advertisements

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. Peter Strøiman
    Replies:
    1
    Views:
    2,375
    Peter Strøiman
    Aug 23, 2005
  2. sahm
    Replies:
    4
    Views:
    43,023
    rel0aded911
    Nov 23, 2009
  3. Richard Heathfield
    Replies:
    7
    Views:
    627
    Barry Schwarz
    Oct 5, 2003
  4. Replies:
    5
    Views:
    351
    Micah Elliott
    Nov 1, 2005
  5. utab

    Words Words

    utab, Feb 16, 2006, in forum: C++
    Replies:
    6
    Views:
    681
    Daniel T.
    Feb 16, 2006
  6. BerlinBrown
    Replies:
    6
    Views:
    7,051
  7. Lasse Edsvik

    replace words with bold words

    Lasse Edsvik, Oct 5, 2003, in forum: ASP General
    Replies:
    9
    Views:
    485
  8. pantagruel
    Replies:
    8
    Views:
    772
    Dr John Stockton
    Jul 22, 2006
Loading...