file data to list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anish Chapagain

    file data to list

    Hi!!
    I am facing problem for extracting file data to the list so as to have
    graph plotted through list.
    my file(eg: data.txt) is having data like this,

    cnt0001a 29000 xretya 01
    cnt0002a 29850 brishal 02
    cnt0003a 31250 kristal 03

    from here, I need to copy data 29000, 29850, 31250 into a single list
    and
    01, 02, 03 to another so as to plot graph using these value.

    regard's
    NepaliWorld
  • bearophileHUGS@lycos.com

    #2
    Re: file data to list

    Anish Chapagain:
    cnt0001a 29000 xretya 01
    cnt0002a 29850 brishal 02
    cnt0003a 31250 kristal 03
    from here, I need to copy data 29000, 29850, 31250 into a single list
    and
    01, 02, 03 to another so as to plot graph using these value.
    This may offer you a starting point:
    >>line = "cnt0003a 31250 kristal 03"
    >>parts = line.split()
    >>parts
    ['cnt0003a', '31250', 'kristal', '03']
    >>parts[1], parts[3]
    ('31250', '03')
    >>map(int, [parts[1], parts[3]])
    [31250, 3]

    Bye,
    bearophile

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: file data to list

      On Thu, 28 Aug 2008 12:11:39 -0700, Anish Chapagain wrote:
      I am facing problem for extracting file data to the list so as to have
      graph plotted through list.
      my file(eg: data.txt) is having data like this,
      >
      cnt0001a 29000 xretya 01
      cnt0002a 29850 brishal 02
      cnt0003a 31250 kristal 03
      >
      from here, I need to copy data 29000, 29850, 31250 into a single list
      and 01, 02, 03 to another so as to plot graph using these value.
      Then work through the tutorial in the documentation, pay attention to
      strings, lists, files, and their methods. The try to implement it and
      come back with some source code and specific questions.

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Emile van Sebille

        #4
        Re: file data to list

        Anish Chapagain wrote:
        Hi!!
        I am facing problem for extracting file data to the list so as to have
        graph plotted through list.
        my file(eg: data.txt) is having data like this,
        >
        cnt0001a 29000 xretya 01
        cnt0002a 29850 brishal 02
        cnt0003a 31250 kristal 03
        >
        from here, I need to copy data 29000, 29850, 31250 into a single list
        and
        01, 02, 03 to another so as to plot graph using these value.
        >
        data = zip(*[xx.split() for xx in open('data.txt' ).read().split( "\n")])

        ....assuming newline separators...

        ....and only 'cuz I like this about zip...

        Emile




        Comment

        • Sion Arrowsmith

          #5
          Re: file data to list

          Emile van Sebille <emile@fenx.com wrote:
          >data = zip(*[xx.split() for xx in open('data.txt' ).read().split( "\n")])
          Files are iterable:

          data = zip(*[xx.rstrip().spl it() for xx in open('data.txt' )])

          saves you creating the extra intermediate list resulting from split("\n").

          --
          \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
          "Frankly I have no feelings towards penguins one way or the other"
          -- Arthur C. Clarke
          her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

          Comment

          Working...