我试图用一个.txt文件为我的游戏制作一个排行榜函数,但在提取列表时,它会将元组转换成字符串
我的守则如下:
#USER INPUT__________________________________________________________
text=input("input: ")
#READING AND PRINTING FILE__________________________________________
a_file = open("textest.txt", "r")
list_of_lists = []
for line in a_file:
stripped_line = line.strip()
line_list = stripped_line.split()
list_of_lists.append(line_list)
a_file.close()
print(list_of_lists)
#INSERTING STUFF IN THE FILE_________________________________________
with open("textest.txt", "a+") as file_object:
file_object.seek(0)
data = file_object.read(100)
if len(data) > 0 :
file_object.write("\n")
file_object.write(text)
该文件的内容如下:
['test1', 60]
['play 1', 5080]
['test2', 60]
['test3', 160]
['fake1', 69420]
但结果是
("['test1', 60]","['play 1', 5080]","['test2', 60]","['test3', 160]","['fake1', 69420]")
ast.literal_eval()
安全地计算表达式节点或包含Python文本的字符串输出:
使用
ast.literal_eval()
将文件解析为列表相关问题 更多 >
编程相关推荐