[英]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.