Reading selected data from text files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ..:: sjf ::..

    Reading selected data from text files

    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 ;-)"
  • Piet van Oostrum

    #2
    Re: Reading selected data from text files

    >>>>> "..:: sjf ::.." <sjf@autograf.p l> (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 <piet@cs.uu.n l>
    URL: http://www.cs.uu.nl/~piet [PGP]
    Private email: P.van.Oostrum@h ccnet.nl

    Comment

    • ..:: sjf ::..

      #3
      Re: Reading selected data from text files

      U¿ytkownik "Piet van Oostrum" <piet@cs.uu.n l> napisa³ w wiadomo¶ci
      news:wzk728inbx .fsf@Ordesa.loc al...[color=blue][color=green][color=darkred]
      > >>>>> "..:: sjf ::.." <sjf@autograf.p l> (SJF) wrote:[/color][/color]
      >
      > 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?[/color]

      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 ;-)"

      Comment

      • Paul McGuire

        #4
        Re: Reading selected data from text files

        "..:: sjf ::.." <sjf@autograf.p l> wrote in message
        news:c1njho$adr $1@nemesis.news .tpi.pl...[color=blue]
        > Hello Pythoners!
        >
        > There are a lot of files containing data such as:
        >
        > file1:
        > 0950 1550
        >
        > file22:
        > 0952 1552
        >[/color]
        <snip>[color=blue]
        > --
        > ..:: sjf ::..
        > "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"[/color]
        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.upd ate( line.split() )
        # if running Python 2.3.1 or later, replace previous line with
        # uniqueElems |= line.split()

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

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


        Good luck,
        -- Paul


        Comment

        • ..:: sjf ::..

          #5
          Re: Reading selected data from text files

          U¿ytkownik "Piet van Oostrum" <piet@cs.uu.n l> napisa³ w wiadomo¶ci
          news:wzk728inbx .fsf@Ordesa.loc al...[color=blue][color=green][color=darkred]
          > >>>>> "..:: sjf ::.." <sjf@autograf.p l> (SJF) wrote:[/color][/color]
          >
          > 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?[/color]

          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 ;-)"

          Comment

          • ..:: sjf ::..

            #6
            Re: Reading selected data from text files

            U¿ytkownik "Paul McGuire" <ptmcg@users.so urceforge.net> napisa³ w wiadomo¶ci
            news:MVK%b.7974 $lS1.1103@fe2.t exas.rr.com...[color=blue]
            > "..:: sjf ::.." <sjf@autograf.p l> wrote in message
            > news:c1njho$adr $1@nemesis.news .tpi.pl...[color=green]
            > > Hello Pythoners!
            > >
            > > There are a lot of files containing data such as:
            > >
            > > file1:
            > > 0950 1550
            > >
            > > file22:
            > > 0952 1552
            > >[/color]
            > <snip>[color=green]
            > > --
            > > ..:: sjf ::..
            > > "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"[/color]
            > 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.upd ate( line.split() )
            > # if running Python 2.3.1 or later, replace previous line with
            > # uniqueElems |= line.split()[/color]
            ^^^^^^^
            OK, but what is that?


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

            Comment

            • Paul McGuire

              #7
              Re: Reading selected data from text files

              "..:: sjf ::.." <sjf@autograf.p l> wrote in message
              news:c1vl6l$lle $1@atlantis.new s.tpi.pl...[color=blue]
              > U¿ytkownik "Paul McGuire" <ptmcg@users.so urceforge.net> napisa³ w[/color]
              wiadomo¶ci[color=blue]
              > news:MVK%b.7974 $lS1.1103@fe2.t exas.rr.com...[color=green]
              > > "..:: sjf ::.." <sjf@autograf.p l> wrote in message
              > > news:c1njho$adr $1@nemesis.news .tpi.pl...[color=darkred]
              > > > Hello Pythoners!
              > > >
              > > > There are a lot of files containing data such as:
              > > >
              > > > file1:
              > > > 0950 1550
              > > >
              > > > file22:
              > > > 0952 1552
              > > >[/color]
              > > <snip>[color=darkred]
              > > > --
              > > > ..:: sjf ::..
              > > > "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"[/color]
              > > 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.upd ate( line.split() )
              > > # if running Python 2.3.1 or later, replace previous line with
              > > # uniqueElems |= line.split()[/color]
              > ^^^^^^^
              > OK, but what is that?
              >
              >[/color]
              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_up date(), and, um, some other update() that I can't recall off
              the top of my head, something like "negative_inter section_update" or
              something.

              -- Paul


              Comment

              Working...