file data to list

A

Anish Chapagain

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
 
B

bearophileHUGS

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
 
M

Marc 'BlackJack' Rintsch

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
 
E

Emile van Sebille

Anish said:
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
 
S

Sion Arrowsmith

Emile van Sebille said:
data = zip(*[xx.split() for xx in open('data.txt').read().split("\n")])

Files are iterable:

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top