Reading selected data from text files

Discussion in 'Python' started by ..:: sjf ::.., Feb 27, 2004.

  1. Hello Pythoners!

    There are a lot of files containing data such as:

    file1:
    0950 1550

    file22:
    0952 1552

    file3:
    1000 1020 1050 1130 1150 1200 1245 1600

    file4:
    1002 1022 1052 1132 1152 1202 1247 1602

    file5:
    1005 1025 1055 1135 1155 1205 1250 1605

    I want to read from them only that data which produce a following sequence
    of data:
    => 0950 0952 1000 1002 1005
    => 1555 1557 1600 1602 1605


    --
    ...:: sjf ::..
    "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
    ..:: sjf ::.., Feb 27, 2004
    #1
    1. Advertising

  2. >>>>> "..:: sjf ::.." <> (SJF) wrote:

    SJF> Hello Pythoners!
    SJF> There are a lot of files containing data such as:

    SJF> file1:
    SJF> 0950 1550

    SJF> file22:
    SJF> 0952 1552

    SJF> file3:
    SJF> 1000 1020 1050 1130 1150 1200 1245 1600

    SJF> file4:
    SJF> 1002 1022 1052 1132 1152 1202 1247 1602

    SJF> file5:
    SJF> 1005 1025 1055 1135 1155 1205 1250 1605

    SJF> I want to read from them only that data which produce a following sequence
    SJF> of data:
    SJF> => 0950 0952 1000 1002 1005
    SJF> => 1555 1557 1600 1602 1605

    How do you get at these values, as 1555 and 1557 aren't in your input files?
    --
    Piet van Oostrum <>
    URL: http://www.cs.uu.nl/~piet [PGP]
    Private email:
    Piet van Oostrum, Feb 27, 2004
    #2
    1. Advertising

  3. U¿ytkownik "Piet van Oostrum" <> napisa³ w wiadomo¶ci
    news:...
    > >>>>> "..:: sjf ::.." <> (SJF) wrote:

    >
    > SJF> Hello Pythoners!
    > SJF> There are a lot of files containing data such as:
    >
    > SJF> file1:
    > SJF> 0950 1550
    >
    > SJF> file22:
    > SJF> 0952 1552
    >
    > SJF> file3:
    > SJF> 1000 1020 1050 1130 1150 1200 1245 1600
    >
    > SJF> file4:
    > SJF> 1002 1022 1052 1132 1152 1202 1247 1602
    >
    > SJF> file5:
    > SJF> 1005 1025 1055 1135 1155 1205 1250 1605
    >
    > SJF> I want to read from them only that data which produce a following
    > sequence SJF> of data:
    > SJF> => 0950 0952 1000 1002 1005
    > SJF> => 1555 1557 1600 1602 1605
    >
    > How do you get at these values, as 1555 and 1557 aren't in your input
    > files?


    ups...
    of course it should be:
    => 0950 0952 1000 1002 1005
    => 1600 1602 1605


    --
    ...:: sjf ::..
    "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
    ..:: sjf ::.., Feb 27, 2004
    #3
  4. ..:: sjf ::..

    Paul McGuire Guest

    "..:: sjf ::.." <> wrote in message
    news:c1njho$adr$...
    > Hello Pythoners!
    >
    > There are a lot of files containing data such as:
    >
    > file1:
    > 0950 1550
    >
    > file22:
    > 0952 1552
    >

    <snip>
    > --
    > ..:: sjf ::..
    > "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"

    Try this:

    import sets
    import os

    uniqueElems = sets.Set()

    testdata = """
    0950 1550
    0952 1552
    1000 1020 1050 1130 1150 1200 1245 1600
    1002 1022 1052 1132 1152 1202 1247 1602
    1005 1025 1055 1135 1155 1205 1250 1605
    """
    for line in testdata.split("\n"):
    uniqueElems.update( line.split() )
    # if running Python 2.3.1 or later, replace previous line with
    # uniqueElems |= line.split()

    print uniqueElems
    elemList = list(uniqueElems)
    elemList.sort()
    print elemList

    # to process all files in directory fileDir
    #
    #for fnam in os.listdir(fileDir):
    # for line in file(fnam):
    # uniqueElems.update( line.split() )


    Good luck,
    -- Paul
    Paul McGuire, Feb 27, 2004
    #4
  5. U¿ytkownik "Piet van Oostrum" <> napisa³ w wiadomo¶ci
    news:...
    > >>>>> "..:: sjf ::.." <> (SJF) wrote:

    >
    > SJF> Hello Pythoners!
    > SJF> There are a lot of files containing data such as:
    >
    > SJF> file1:
    > SJF> 0950 1550
    >
    > SJF> file22:
    > SJF> 0952 1552
    >
    > SJF> file3:
    > SJF> 1000 1020 1050 1130 1150 1200 1245 1600
    >
    > SJF> file4:
    > SJF> 1002 1022 1052 1132 1152 1202 1247 1602
    >
    > SJF> file5:
    > SJF> 1005 1025 1055 1135 1155 1205 1250 1605
    >
    > SJF> I want to read from them only that data which produce a following
    > sequence SJF> of data:
    > SJF> => 0950 0952 1000 1002 1005
    > SJF> => 1555 1557 1600 1602 1605
    >
    > How do you get at these values, as 1555 and 1557 aren't in your input
    > files?


    ups...
    of course it should be:
    => 0950 0952 1000 1002 1005
    => 1550 1552 1600 1602 1605


    --
    ...:: sjf ::..
    "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
    ..:: sjf ::.., Mar 1, 2004
    #5
  6. U¿ytkownik "Paul McGuire" <> napisa³ w wiadomo¶ci
    news:MVK%b.7974$...
    > "..:: sjf ::.." <> wrote in message
    > news:c1njho$adr$...
    > > Hello Pythoners!
    > >
    > > There are a lot of files containing data such as:
    > >
    > > file1:
    > > 0950 1550
    > >
    > > file22:
    > > 0952 1552
    > >

    > <snip>
    > > --
    > > ..:: sjf ::..
    > > "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"

    > Try this:
    >
    > import sets
    > import os
    >
    > uniqueElems = sets.Set()
    >
    > testdata = """
    > 0950 1550
    > 0952 1552
    > 1000 1020 1050 1130 1150 1200 1245 1600
    > 1002 1022 1052 1132 1152 1202 1247 1602
    > 1005 1025 1055 1135 1155 1205 1250 1605
    > """
    > for line in testdata.split("\n"):
    > uniqueElems.update( line.split() )
    > # if running Python 2.3.1 or later, replace previous line with
    > # uniqueElems |= line.split()

    ^^^^^^^
    OK, but what is that?


    --
    ...:: sjf ::..
    "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
    ..:: sjf ::.., Mar 1, 2004
    #6
  7. ..:: sjf ::..

    Paul McGuire Guest

    "..:: sjf ::.." <> wrote in message
    news:c1vl6l$lle$...
    > U¿ytkownik "Paul McGuire" <> napisa³ w

    wiadomo¶ci
    > news:MVK%b.7974$...
    > > "..:: sjf ::.." <> wrote in message
    > > news:c1njho$adr$...
    > > > Hello Pythoners!
    > > >
    > > > There are a lot of files containing data such as:
    > > >
    > > > file1:
    > > > 0950 1550
    > > >
    > > > file22:
    > > > 0952 1552
    > > >

    > > <snip>
    > > > --
    > > > ..:: sjf ::..
    > > > "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"

    > > Try this:
    > >
    > > import sets
    > > import os
    > >
    > > uniqueElems = sets.Set()
    > >
    > > testdata = """
    > > 0950 1550
    > > 0952 1552
    > > 1000 1020 1050 1130 1150 1200 1245 1600
    > > 1002 1022 1052 1132 1152 1202 1247 1602
    > > 1005 1025 1055 1135 1155 1205 1250 1605
    > > """
    > > for line in testdata.split("\n"):
    > > uniqueElems.update( line.split() )
    > > # if running Python 2.3.1 or later, replace previous line with
    > > # uniqueElems |= line.split()

    > ^^^^^^^
    > OK, but what is that?
    >
    >

    In 2.3.1, the Set() class defines a "|=" operator that will add all elements
    of a list to the set.

    Also, update() is deprecated in 2.3.1 in favor of union_update(),
    intersection_update(), and, um, some other update() that I can't recall off
    the top of my head, something like "negative_intersection_update" or
    something.

    -- Paul
    Paul McGuire, Mar 1, 2004
    #7
    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. Iain
    Replies:
    3
    Views:
    917
  2. Nathan Sokalski
    Replies:
    0
    Views:
    595
    Nathan Sokalski
    Oct 5, 2008
  3. John
    Replies:
    5
    Views:
    971
    dorayme
    Mar 1, 2010
  4. mldardy
    Replies:
    0
    Views:
    952
    mldardy
    Sep 28, 2010
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    280
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page