黑崎一护 发表于 2018-9-19 09:01:41

sequence item 1: expected string, list found

我从文件中提取了一些数据,并准备将其写入另一个文件。但我的程序返回错误:
sequence item 1: expected string, list found
报错的代码如下
file = open('file1.txt','r')
file2 = open('file2.txt','w')
buffer = []
rec = file.readlines()
for line in rec :
    field = line.split()
    term1 = field
    buffer.append(term1)
    term2 = field
    buffer.append
    file2.write(buffer)# <== error
file.close()
file2.close()

马猴烧酒 发表于 2018-9-19 09:03:52

试试str.join:
file2.write(' '.join(buffer))

I_Like_AI 发表于 2018-9-19 09:05:02

file2.write( str(buffer) )
说明: str(anything)将任何python对象转换为其字符串表示形式。类似于你得到的输出print(anything),
页: [1]
查看完整版本: sequence item 1: expected string, list found