[英]TypeError: expected a character buffer object
我正在尝试将列表的列表写入新文件,但是出现此错误:
追溯(最近一次通话最后一次):dowork()中文件“”,第1行,dowork中文件“ C:\\ Python27 \\ work \\ accounting \\ formatting quickbooks file \\ sdf.py”,第11行WriteFile()文件“ C” :\\ Python27 \\ work \\ accounting \\ formatting quickbooks file \\ sdf.py“,在WriteFile f.write(thefile)TypeError中的第71行:应为字符缓冲区对象
如何将列表的列表写入文件?
这就是我写的方式:
def WriteFile():
global thefile
f = open("output"+'.csv','w')
f.seek(0)
f.write(thefile)
f.close()
如果需要,这里是完整的资源:
import csv
thefile = []
output = []
def dowork():
sourceFile='e.csv'
thefile=ReadFile(sourceFile)
CleanFile(sourceFile)
ProcessFile()
WriteFile()
def ReadFile(filename):
return list(csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"'))[1:]
def CleanFile(sourceFile):
global thefile
thefiletmp=[]
for i, line in enumerate(thefile):
if line[2]=='':
del thefile[i]
else:
thefiletmp.append(line[4:])
thefile=thefiletmp
def ProcessFile():
global thefile
iCompany=1
iNum=0
iDate=2
iAging=3
iBalance=4
COMPANIES=GetDistinctValues(1)
mytemparray=[]
mytempfile=[]
for company in COMPANIES:
for line in thefile:
if line[iCompany]==company:
mytemparray.append(line[iCompany])
mytemparray.append(line[iNum])
mytemparray.append(line[iDate])
if line[2] in range(0,31):
mytemparray.append(line[iBalance])
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append('0')
if line[2] in range(31,61):
mytemparray.append('0')
mytemparray.append(line[iBalance])
mytemparray.append('0')
mytemparray.append('0')
if line[2] in range(61,91):
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append(line[iBalance])
mytemparray.append('0')
if line[2] >90:
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append(line[iBalance])
mytempfile.append(mytemparray)
mytemparray=[]
thefile=mytempfile
def WriteFile():
global thefile
f = open("output"+'.csv','w')
f.seek(0)
f.write(thefile)
f.close()
def GetDistinctValues(theColumn):
return sorted(list(set(line[theColumn] for line in thefile)))
错误消息的意思是您不能将列表写入文件,只能写入“字符缓冲区对象”,即字符串或其他类似字符串的内容。
如果您只想以将列表打印到控制台的相同方式将列表写入文件,则可以编写str(thefile)
或repr(thefile)
(甚至在print
中使用重定向语法而不是使用file.write
)。
但是您正在使用csv
模块读取输入,并且大概希望输出具有相同的格式,因此您可能也想使用csv
来编写它。
您正在这样阅读:
list(csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"'))[1:]
所以这样写:
csv.writer(open('foo.csv', 'wb'), delimiter=',', quotechar='"').writerows(thefile)
我应该提到,我一开始就不会像这样构造代码。 我会做这样的事情:
with open('input.csv', 'rb') as infile, open('output.csv', 'wb') as outfile:
incsv = csv.reader(infile, delimiter=',', quotechar='"')
outcsv = csv.writer(outfile, delimiter=',', quotechar='"')
incsv.read() # skip first line
for line in incsv:
if line[3] != '':
outcsv.write(ProcessLine(line))
您无法将列表写入文件; 您只能写一个字符串。 以某种方式将列表转换为字符串,或者遍历列表并一次写入一个元素。 或者您正在使用csv
模块,请使用该模块写入文件。
thefile
一下,调用文件以外的其他东西(例如列表)必然会引起混乱。
该文件是列表的列表,而不是字符缓冲区。
for sublist in thefile:
f.write("".join(sublist)) # perhaps
这里有一些不好的地方,使用全局命名列表文件,...
(正确答案是阿巴内特的答案)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.