我在.txt
文件中有这个表单的列表:student1, 2, 3 | student2, 1, 4
我需要在程序中调用它,将[student3, 3, 5]
添加到列表中,然后将它保存回.txt
文件中。我这样做了,但我的列表最后看起来是这样的:[[''], ['Student3', '3', '5']]
f = open('classe1.txt','r')
liste = f.read()
liste = liste.split('|')
n = len(liste)
for i in range (n):
liste[i] = liste[i].split(',')
liste.append(['Student3','3','5'])
liste = str(liste)
f = open('classe1.txt','w')
f.write(liste)
你会怎么做? 谢谢你
我不完全确定我是否正确理解了您的问题,但是要将字符串附加到文本文件中,您可以使用
a
,即:如果需要将python
object
写入文件,可以使用json load and dump或Pickle相关问题 更多 >
编程相关推荐