how to read list from file

  • Thread starter Harvey Greenberg
  • Start date
H

Harvey Greenberg

I am looping as for L in file.readlines(), where file is csv.

L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first item is a dir and 2nd is a list, so parsing with split doesn't work. Is there a way to convert L, which is a string, to the list of 3 items I want?
 
T

Tim Chase

I am looping as for L in file.readlines(), where file is csv.

L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that
the first item is a dir and 2nd is a list, so parsing with split
doesn't work. Is there a way to convert L, which is a string, to
the list of 3 items I want?

sounds like you want ast.literal_eval():

Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
s = "[{'a':1, 'b':2}, [1,2,3], 10]"
import ast
print repr(ast.literal_eval(s))
[{'a': 1, 'b': 2}, [1, 2, 3], 10]

-tkc
 
R

Roy Smith

Harvey Greenberg said:
I am looping as for L in file.readlines(), where file is csv.

L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first
item is a dir and 2nd is a list, so parsing with split doesn't work. Is
there a way to convert L, which is a string, to the list of 3 items I want?

I hate to recommend it (since it's bad practice for a number of
legitimate reasons), but passing your string to eval() will get you what
you want.

It's also very close to being valid JSON syntax, the only difference
being the use of single instead of double quotes. You might want to
just turn it into JSON by substituting the right kind of quotes.

json.loads("[{'a':1, 'b':2}, [1,2,3], 10]".replace("'", '"'))
[{u'a': 1, u'b': 2}, [1, 2, 3], 10]
 
H

Harvey Greenberg

I am looping as for L in file.readlines(), where file is csv.
L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that
the first item is a dir and 2nd is a list, so parsing with split
doesn't work. Is there a way to convert L, which is a string, to
the list of 3 items I want?



sounds like you want ast.literal_eval():



Python 2.7.3 (default, Jan 2 2013, 13:56:14)

[GCC 4.7.2] on linux2

Type "help", "copyright", "credits" or "license" for more

information.
s = "[{'a':1, 'b':2}, [1,2,3], 10]"
import ast
print repr(ast.literal_eval(s))

[{'a': 1, 'b': 2}, [1, 2, 3], 10]



-tkc

that didn't work. printing it looks like the list because it's the input, but try printing len(repr(ast.literal_eval(s))). It should give 3, but it gives 72 (number of chars).
 
H

Harvey Greenberg

On 2013-10-05 18:08, Harvey Greenberg wrote:
I am looping as for L in file.readlines(), where file is csv.
L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that
the first item is a dir and 2nd is a list, so parsing with split
doesn't work. Is there a way to convert L, which is a string, to
the list of 3 items I want?
sounds like you want ast.literal_eval():
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
s = "[{'a':1, 'b':2}, [1,2,3], 10]"
import ast
print repr(ast.literal_eval(s))
[{'a': 1, 'b': 2}, [1, 2, 3], 10]
-tkc



that didn't work. printing it looks like the list because it's the input, but try printing len(repr(ast.literal_eval(s))). It should give 3, but it gives 72 (number of chars).

None of the responses worked; after import json, I used:

for line in inputFile.readlines():
L = json.loads(line.replace("",""))
print L, len(L)

I get error. I probably misunderstood how to implement these suggestions, but I wrote a list to a csv file whose members have lists. I now want to read them (in another program) and end up with the origianl list.
 
R

Ravi Sahni

Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
s = "[{'a':1, 'b':2}, [1,2,3], 10]"
import ast
print repr(ast.literal_eval(s))
[{'a': 1, 'b': 2}, [1, 2, 3], 10]



-tkc

that didn't work. printing it looks like the list because it's the input, but try printing len(repr(ast.literal_eval(s))). It should give 3, but it gives 72 (number of chars).

Please to remove the repr and try again?
Thank you!
 
H

Harvey Greenberg

I am looping as for L in file.readlines(), where file is csv.



L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first item is a dir and 2nd is a list, so parsing with split doesn't work. Is there a way to convert L, which is a string, to the list of 3 items I want?

Yay!!!! It worked. Thanks!
 
M

Mark Lawrence

I am looping as for L in file.readlines(), where file is csv.



L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first item is a dir and 2nd is a list, so parsing with split doesn't work. Is there a way to convert L, which is a string, to the list of 3 items I want?

Yay!!!! It worked. Thanks!

Very pleased to know, but if you need to post again would you be kind
enough to read this first, thanks
https://wiki.python.org/moin/GoogleGroupsPython

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
J

Jussi Piitulainen

Harvey said:
s = "[{'a':1, 'b':2}, [1,2,3], 10]"
import ast
print repr(ast.literal_eval(s))
[{'a': 1, 'b': 2}, [1, 2, 3], 10]

that didn't work. printing it looks like the list because it's the
input, but try printing len(repr(ast.literal_eval(s))). It should
give 3, but it gives 72 (number of chars).

Not sure what the "print repr( )" is meant to achieve here, but I
think you should be able to see through it:
ast.literal_eval(s) [{'a': 1, 'b': 2}, [1, 2, 3], 10]
len(ast.literal_eval(s)) 3
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,531
Members
45,004
Latest member
KetoBoostGummies

Latest Threads

Top