【发布时间】:2015-10-02 19:51:15
【问题描述】:
我尝试了一些方法,但它们要么不起作用,要么变成列表中每个项目的字母。 例如,这个(用于编写列表):
with open("data.txt", 'w') as f:
for s in data:
f.write(s + '\n')
还有这个(用于阅读列表):
try:
with open("data.txt", 'r') as f:
data = [Line.rstrip('\n') for Line in f]
except FileNotFoundError:
print ("There is nothing to show")
else:
print (data)
当我想要在文件中的列表是“['Bob:6', 'Dave:4']”时打印出来:
['B', 'o', 'b', ':', '6', 'D', 'a', 'v', 'e', ':', '4']
基本上,我希望它能够正确读取,并正确分隔不同的值。
【问题讨论】:
-
你的代码在哪里,有什么问题?
-
那么...
file.writelines? -
你想要一些严肃的东西,不要伤害自己。将列表写入文件时,您是否尝试过循环遍历列表?
-
你能用你尝试过的所有方法来启发我们人类吗?
-
如果您添加您尝试过的方法,您将有更好的机会为工作解决方案找到方向。
标签: python string list file python-3.x