CSV模块的编写者不会让我写出二进制文件 [英] CSV Module's writer won't let me write binary out

查看:73
本文介绍了CSV模块的编写者不会让我写出二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在打开文件时只使用'w'标记,但是它使行间距加倍,导致读取无法正常进行.因此,我发现更改为"wb"将是正确的格式.现在,我正在使用"wb"标志,因此无法使csv.writer.writerow()正常工作.我已经对所有字符串进行了编码,但对为什么继续收到此错误一无所知.我看到的所有问题都说b'string here'或myString.encode('ascii')解决了我收到的错误,但并不是为我解决.这是我所拥有的:

I tried to just use the 'w' tag while opening the file, but it double spaced the lines which caused the read to not work. So I found that changing to 'wb' will be the correct formatting. Now that I am using the 'wb' flag I can't get the csv.writer.writerow() to work. I have encoded all my strings and am lost as to why I keep getting this error. All the questions I see say that b'string here' or myString.encode('ascii') solves the error I get, but it is not solving it for me. Here is what I have:

    dataWriter = csv.writer(open(fileName, 'wb'))
    for i in range(self.ui.table.rowCount()):
        rowData = [self.ui.table.item(i,0).text().encode('utf-8')\
        ,self.ui.table.item(i,1).text().encode('utf-8')\
        ,self.ui.table.item(i,2).text().encode('utf-8')\
        ,self.ui.table.item(i,3).text().encode('utf-8')\
        ,self.ui.table.item(i,4).text().encode('utf-8')]
        dataWriter.writerow(rowData)

我认为可以使用,但是仍然会出现以下错误: "TypeError:必须为字节或缓冲区,不能为str" 在"dataWriter.writerow(rowData)"行上.

Which I figured would work but it still gives me the following error: "TypeError: must be bytes or buffer, not str" on the line "dataWriter.writerow(rowData).

任何帮助将不胜感激. 谢谢.

Any help would be apreciated. Thank you.

推荐答案

您似乎正在运行Python3.x. 关于对csv文件使用二进制模式的建议适用于Python 2.x. 3.x不需要编解码器模块-打开文件时只需使用encoding=whatever. 3.x所需的是使用 newline=''打开文件.尽管没有书面记录(已提交错误报告),但它适用于阅读和书写.在解决了延误问题之后,这将起作用:

You appear to be running Python 3.x. Advice about using binary mode for csv files applies to Python 2.x. The codecs module is not required for 3.x -- just use encoding=whatever when you open the file. What is needed for 3.x is that the file be opened with newline=''. This applies to both reading and writing, although it is not documented for writing (bug report has been submitted). After sorting out your doble-spacing problem, this will work:

import csv
data = [
    ['\xfforick', 123.456],
    ['polonius', 987.564],
    ]
with open('demo.csv', 'w', newline='', encoding='utf8') as f:
    writer = csv.writer(f)
    for row in data:
        writer.writerow(row)

输出文件的内容:

>>> open('demo.csv', 'rb').read()
b'\xc3\xbforick,123.456\r\npolonius,987.564\r\n'
>>>

建议:考虑代码的可读性...而不是

Suggestion: give some consideration to legibility of your code ... instead of

for i in range(self.ui.table.rowCount()):
    rowData = [self.ui.table.item(i,0).text().encode('utf-8')\
    ,self.ui.table.item(i,1).text().encode('utf-8')\
    ,self.ui.table.item(i,2).text().encode('utf-8')\
    ,self.ui.table.item(i,3).text().encode('utf-8')\
    ,self.ui.table.item(i,4).text().encode('utf-8')]
    dataWriter.writerow(rowData)

尝试

table = self.ui.table
for i in range(table.rowCount()):
    row = [table.item(i, j).text() for j in range(5)]
    writer.writerow(row)

这篇关于CSV模块的编写者不会让我写出二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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