Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python csv.writer遇到类型错误_Python_Csv_Python 3.x - Fatal编程技术网

Python csv.writer遇到类型错误

Python csv.writer遇到类型错误,python,csv,python-3.x,Python,Csv,Python 3.x,我正在编写一个脚本来帮助我处理.csv文件。目标是读取指定的.csv,然后将数据拆分为几个临时文件以进行进一步操作。input.csv中的一个空行(空字符串列表)表示我要拆分数据的位置 如果我的代码与PEP 8中的任何内容冲突,我深表歉意。Python(就这一点而言,一般来说编码)对我来说还是很新的 import os import csv import tempfile def importFromCSV(filepath): print("Reading data from",os

我正在编写一个脚本来帮助我处理.csv文件。目标是读取指定的.csv,然后将数据拆分为几个临时文件以进行进一步操作。input.csv中的一个空行(空字符串列表)表示我要拆分数据的位置

如果我的代码与PEP 8中的任何内容冲突,我深表歉意。Python(就这一点而言,一般来说编码)对我来说还是很新的

import os
import csv
import tempfile

def importFromCSV(filepath):
    print("Reading data from",os.path.basename(filepath))
    datalist = []
    with open(filepath) as csvfile:
        file_dialect = csv.Sniffer().sniff(csvfile.readline(),[',',';',':','\t','.'])
        csvfile.seek(0)
        filereader = csv.reader(csvfile,dialect = file_dialect)
        for row in filereader:
            datalist.append(row)
    return datalist

def SplitToTemp(datalist, target_dir):
    tmpnamelist = []
    templist = []
    for item in datalist:
        if item[0] != '':
            templist.append(item)
        else:
            del item
            f = tempfile.NamedTemporaryFile(delete = False, dir = target_dir)
            tmpnamelist.append(f.name)
            dw = csv.writer(f, delimiter = ',', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
            for row in templist:
                dw.writerow(row)
            f.close()
            templist = []
    return tmpnamelist

###############################################################################
pathname = os.path.normpath('C:/Python33/myprograms/myclassandfx/BenchLink/blrtest.csv')
tempdir = tempfile.mkdtemp(dir = os.path.normpath('c:/users/'+os.getlogin()+'/desktop'))

filedata = import_from_csv(pathname)
tempnames = SplitToTemp(filedata, tempdir)
但是,当我运行代码时,我遇到以下情况:

Traceback (most recent call last):
  File "C:\Python33\myprograms\myclassandfx\BenchLink\BenchLinkReader_classless.py", line 56, in <module>
    tempnames = SplitToTemp(filedata, tempdir)
  File "C:\Python33\myprograms\myclassandfx\BenchLink\BenchLinkReader_classless.py", line 45, in     SplitToTemp
    dw.writerow(row)
TypeError: 'str' does not support the buffer interface
回溯(最近一次呼叫最后一次):
文件“C:\Python33\myprograms\myclassandfx\BenchLink\benchlinkrader\u classless.py”,第56行,在
tempnames=SplitToTemp(filedata,tempdir)
文件“C:\Python33\myprograms\myclassandfx\BenchLink\BenchLinkReader\u classless.py”,第45行,在SplitToTemp中
dw.writerow(行)
TypeError:“str”不支持缓冲区接口
让我困惑的是,当我打印(temp)时,仍然会得到一个列表列表


我做错了什么?

默认情况下,
tempfile.NamedTemporaryFile()
对象以二进制模式打开;引述:

模式参数默认为
'w+b'
,这样创建的文件可以在不关闭的情况下进行读写。使用二进制模式,使其在所有平台上的行为一致,而不考虑存储的数据

您需要改为以文本模式打开它:

f = tempfile.NamedTemporaryFile(mode='w+', delete=False, dir=target_dir)

dw.writerow()
方法尝试将unicode
str
值写入仅支持
字节的文件对象(支持缓冲区接口的对象)时,会引发错误。

谢谢!我想我应该更仔细地看一下
tempfile
文档。。。