TypeError: expected a character buffer object

拈花ヽ惹草 提交于 2019-12-19 05:56:15

问题


I am trying to write a list of a list to a new file, but I am getting this error:

Traceback (most recent call last): File "", line 1, in dowork() File "C:\Python27\work\accounting\formatting quickbooks file\sdf.py", line 11, in dowork WriteFile() File "C:\Python27\work\accounting\formatting quickbooks file\sdf.py", line 71, in WriteFile f.write(thefile) TypeError: expected a character buffer object

How do I write a list of a list to a file?

This is how i am writing:

def WriteFile():
    global thefile
    f = open("output"+'.csv','w')
    f.seek(0)
    f.write(thefile)
    f.close()

and here is the complete source if you need it:

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)))

回答1:


What the error message is saying is that you can't write a list to a file, only "a character buffer object", meaning a string or something else that acts a lot like a string.

If you just want to write the list to the file in the same way you'd print them to the console, you can write str(thefile) or repr(thefile) (or even use the redirect syntax in print instead of using file.write).

But you're using the csv module to read the input, and presumably want the output in the same format, so you probably want to use csv to write it too.

You're reading like this:

list(csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"'))[1:]

So write like this:

csv.writer(open('foo.csv', 'wb'), delimiter=',', quotechar='"').writerows(thefile)

I should mention that I wouldn't structure the code like this in the first place; I'd do something like this:

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))



回答2:


You can't write a list to a file; you can only write a string. Convert the list to a string in some way, or else iterate over the list and write an element at a time. Or you're using the csv module, use that to write to the file.

Calling something other than a file (such as a list) thefile is bound to lead to confusion, just by the way.




回答3:


thefile is a list of lists, not a character buffer.

for sublist in thefile:
    f.write("".join(sublist))  # perhaps

there is some bad here, using global, naming a list thefile, ...

(The correct answer is abarnert's).



来源:https://stackoverflow.com/questions/12592544/typeerror-expected-a-character-buffer-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!