TypeError:期望一个字符缓冲区对象 [英] TypeError: expected a character buffer object

查看:175
本文介绍了TypeError:期望一个字符缓冲区对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个列表的列表到一个新的文件,但我得到这个错误:

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


Traceback最近调用最后):文件,第1行,在

dowork()文件C:\Python27\work\accounting\formatting quickbooks file\sdf.py,行11,in dowork
WriteFile()文件C:\Python27\work\accounting\formatting quickbooks file\sdf.py,第71行,在WriteFile中
f.write )TypeError:expected a character buffer object

如何将列表列表写入文件? >

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

这是我写的:

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


推荐答案

错误消息说的是,你不能写一个文件列表,只有字符缓冲对象 ,这意味着一个字符串或其他非常像字符串的东西。

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.

如果你只想将列表写入文件,到控制台,你可以写 str(thefile) repr(thefile) print 而不是使用 file.write )。

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

但是你使用 csv 模块来读取输入,并且可能想要输出格式相同,所以你可能想使用 csv 来写。

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.

您正在这样阅读:

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

这样写:

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

这篇关于TypeError:期望一个字符缓冲区对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆