python将值从txt文件加载到变量

python将值从txt文件加载到变量,python,Python,txt文件如下所示: a.txt ['a','b','c'] #single line txt file 我想做的是,在Python命令行中: >> f = open('a.txt','r') >> a = f.readline() # want to put the list in to a, same as "a = ['a','b','c']" 我该怎么办? python新手,想不出主意 提前谢谢 这是假设您的文件中有此文件…”a b c:“ 这是假设

txt文件如下所示:

a.txt
['a','b','c']   #single line txt file
我想做的是,在Python命令行中:

>> f = open('a.txt','r')
>> a = f.readline()  # want to put the list in to a, same as "a = ['a','b','c']"
我该怎么办? python新手,想不出主意


提前谢谢

这是假设您的文件中有此文件…”a b c:“


这是假设您的文件中有此文件……”['a'、'b'、'c']”: 这样做


这是正确的代码:open(“test.txt”,“r”)为f:a=f.read()b=a.strip()c=eval(b)print 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(" ")
>>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(",")