Hi Everyone,
I am trying to read a comma-delimitted list (aaa,bbb,ccc) from a text
file and assign those values to a list, x, such that:
x = [aaa, bbb, ccc]
The code that I have come up with looks like this:
x = []
f = open(r'c:\test.txt', 'r')
x.extend(f.readlines())
x
['aaa,bbb,ccc']
If
On Thu, Jul 31, 2008 at 8:07 AM, S Python [EMAIL PROTECTED] wrote:
Hi Everyone,
I am trying to read a comma-delimitted list (aaa,bbb,ccc) from a text
file and assign those values to a list, x, such that:
x = [aaa, bbb, ccc]
The code that I have come up with looks like this:
x = []
f
If your list is in the format:
aaa,bbb,ccc
You can use
foo = in_file.readline.split(',')
--
Amin Rainmaker---BeginMessage---
Hi Everyone,
I am trying to read a comma-delimitted list (aaa,bbb,ccc) from a text
file and assign those values to a list, x, such that:
x = [aaa, bbb, ccc]
The code
You can use
foo = in_file.readline.split(',')
--
Amin Rainmaker
From: S Python [EMAIL PROTECTED]
Date: 1 August 2008 1:07:22 AM
To: tutor@python.org
Subject: [Tutor] Reading List from File
Hi Everyone,
I am trying to read a comma-delimitted list (aaa,bbb,ccc) from
a text file and assign those
S Python wrote:
Hi Everyone,
I am trying to read a comma-delimitted list (aaa,bbb,ccc)
In this case, the standard library provides csv for parsing comma
separated files. It's not the same as rolling your own, but it is made
specifically for this use case...
Emile
Emile is right, in python you can do most of the stuff yourself by hand
coding it, or you can use pre-made bullet proof and ready to go modules,
here you can go for the csv module that comes part of the standard library.
import csv
myfile = open('file', 'r') # open file for reading
data =
Monika Jisswel wrote:
Emile is right, in python you can do most of the stuff yourself by hand
coding it, or you can use pre-made bullet proof and ready to go modules,
here you can go for the csv module that comes part of the standard library.
Yes -- and you'll avoid the pitfalls of dealing
Hi Everyone,
Thanks for the variety of responses in such a short amount of time.
This distribution list is incredible.
Sorry for the delayed reply as I wanted to test what everyone
suggested, so here goes:
---
@Amin: I tried your suggestion, but perhaps I don't
oops it is reader not Reader (all lower case), so this line : data =
csv.Reader(myfile, delimeter = ',')
should be data = csv.reader(myfile, delimeter = ',')
2008/7/31 S Python [EMAIL PROTECTED]
Hi Everyone,
Thanks for the variety of responses in such a short amount of time.
This
Monika,
Thanks for your help. I got it to work using the following (also had
to spell delimiter):
import csv
myfile = open(r'c:\test.txt', 'r')
data = csv.reader(myfile, delimiter=',')
print data
_csv.reader object at 0x00D41870
for item in data:
print item
['aaa',
S Python wrote:
f = open(r'C:\test.txt', 'r')
foo = f.readline.split(',')
readline is the function/method name
readline() executes that function/method and returns a value
try typing in 'type(f.readline)' vs 'type(f.readline())'
you can't .split() a function but you may split its return
Emile is rigth, there should be a () there.
I'm sorry, im writing this from my cellphone and there's not a pc around XD.
I didn,t know about the csv module either and had to do over complicated things
to deal with embedded commas, thx for that too :).
--
Amin Rainmaker---BeginMessage---
S
Date: Thu, 31 Jul 2008 11:34:56 -0700
Subject: Re: [Tutor] Reading List from File
S Python wrote:
f = open(r'C:\test.txt', 'r')
foo = f.readline.split(',')
readline is the function/method name
readline() executes that function/method and returns a value
try typing in 'type(f.readline
13 matches
Mail list logo