-2

the txt file is like the following:

a.txt
['a','b','c']   #single line txt file

What I want to do is, in Python commandline:

>> f = open('a.txt','r')
>> a = f.readline()  # want to put the list in to a, same as "a = ['a','b','c']"

What should I do? New to python, can't think of an idea.

Thanks in advance!

Michael
  • 53
  • 1
  • 8

1 Answers1

0

this is assuming you have this in your file..."a b c" :

>>f = open('a.txt','r')
>> a = f.readline()  # want to put the list in to a, same as "a = ['a','b','c']"
>>#add this, it splits your input into array
>>a = a.split(" ")

this is assuming you have this in your file..."['a','b','c']": do this

>>f = open('a.txt','r')
>> a = f.readline()  # want to put the list in to a, same as "a = ['a','b','c']"
>>#add this, it splits your input into array
>>a = a[1:len(a)-1]
>>a = a.split(",")
omkaartg
  • 2,676
  • 1
  • 10
  • 21
  • this is the right code: with open("test.txt",'r') as f: a = f.read() b = a.strip() c = eval(b) print c – Michael Dec 11 '17 at 08:35