我有一个文件,我想将其导入到数组中,但将每个条目作为索引,以便我可以调用每个特定的条目。
文件(testing_array.txt):
["zero", "one", "two", "three", "four", "five"]
脚本:
f = open('testing_array.txt').read()
array = [f]
print (array[0])
print (array[1])
输出:
["zero", "one", "two", "three", "four", "five"]
Traceback (most recent call last):
File "testing_array.py", line 4, in <module>
print (array[1])
IndexError: list index out of range
我已经尝试了一个 for 循环来.insert 每个索引的每个条目,但没有成功。我 3 天前才开始编写 python 脚本,所以如果我忽略了一些基本的东西,我深表歉意。任何帮助将不胜感激,谢谢。
请您参考如下方法:
尝试使用 json 序列化程序(它与 list 的语法相同)。
import json
my_list = json.loads('["zero", "one", "two", "three", "four", "five"]')
