我有 3 个列表:
AList = ['name','name2']
BList = ['desg1','desg2']
InList = [1,2]
我正在使用以下代码片段将其写入文本文件:
fo = open(filepath, "w")
for i in zip(AList,BList,InList):
lines=fo.writelines(','.join(i) + '\n')
但我收到以下错误:
TypeError: sequence item 2: expected string, int found
如何将值写入带有换行符的文本文件。
join
需要字符串项,但您已在 InList 中进行了 int 处理。因此,要么在使用 join 之前将它们转换为字符串,要么您可以这样做:
AList = ['name','name2']
BList = ['desg1','desg2']
InList = ['1','2']
fo = open("a.txt", "w")
for i in range(len(AList)):
dataToWrite = ",".join((AList[i], BList[i], str(InList[i]))) + '\n'
lines=fo.writelines(dataToWrite)
我是一名优秀的程序员,十分优秀!