删除CSV输出中的'b'标志 [英] Remove 'b' flag in CSV output
问题描述
我正在尝试将输出写入Python 3.4中的CSV文件,但是CSV文件始终包含'b'标志.例如,b'The text output1',b'The text output2',...我想知道是否有一种方法可以摆脱'b'标志.我了解这在Python 2.X中不是问题.
I am trying to write the outputs to a CSV file in Python 3.4 but the CSV file always contains 'b' flags. For example, b'The text output1', b'The text output2',... I am wondering if there is a way to get rid of the 'b' flags. I understand that this is not an issue in Python 2.X.
这是我使用的代码
with open('test.csv', 'w') as f:
writer = csv.DictWriter(f, ['field'], extrasaction='ignore')
writer.writeheader()
test_text = mongo.test.find({'text': text})
for t in test_text
writer.writerow({i:v.encode('utf') for i,v in t.items()})
非常感谢
------更新-------------
------Updates-----------
非常感谢Tim Pietzcker,John Zwinck和Warren Weckesser提供有用的评论和答案.根据沃伦的建议,如果我将代码更改为
Thanks very much for Tim Pietzcker, John Zwinck, and Warren Weckesser providing helpful comments and answers. Per Warren's suggestions, if I change my codes to
import csv
data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']
with open('test.csv', 'w') as f:
writer = csv.writer(f)
for item in data:
writer.writerow([item])
我会收到错误消息
UnicodeEncodeError: 'charmap' codec can't encode character '\u03d5' in position 0: character maps to <undefined>
如果我将代码更改为
import csv
data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']
with open('test.csv', 'w') as f:
writer = csv.writer(f)
for item in data:
writer.writerow([item.encode('utf')])
我将获得带有'b'标志的输出
I will get outputs with 'b' flags
b'\xcf\x95oo'
b'b\xc4\x81r'
关于这种情况如何发生以及如何解决该问题的任何想法?再次感谢.
Any thoughts on how this is happening and how I might be able to fix it? Thanks again.
------更新2 -----------
------Updates 2-----------
非常感谢沃伦的解决方案.以下代码有效!
Thanks very much for Warren's solution. The following codes worked!
import csv
data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']
with open('test.csv', 'w', encoding='utf8') as f:
writer = csv.writer(f)
for item in data:
writer.writerow([item])
推荐答案
不要自己对字符串进行显式编码.让作者来照顾它.例如,这段代码:
Don't explicitly encode the strings yourself; let the writer take care of it. For example, this code:
import csv
data = [chr(0x03d5) + 'oo', 'b' + chr(0x0101) + 'r']
with open('test.csv', 'w') as f:
writer = csv.writer(f)
for item in data:
writer.writerow([item])
写入文件
ϕoo
bār
具有UTF-8编码(至少在我的系统上是这样,其中locale.getpreferredencoding(False)
返回'UTF-8'
).要使编码明确,可以在调用中将编码设置为open
:
with UTF-8 encoding (at least it does on my system, where locale.getpreferredencoding(False)
returns 'UTF-8'
). To make the encoding explicit, you can set the encoding in the call to open
:
with open('test.csv', 'w', encoding='utf8') as f:
如果最后一行更改为writer.writerow([item.encode('utf')])
(将字符串转换为bytes
),则会产生
If the last line is changed to writer.writerow([item.encode('utf')])
(which converts the strings to bytes
), it produces
b'\xcf\x95oo'
b'b\xc4\x81r'
在您的示例中,尝试更改此行:
In your example, try changing this line:
writer.writerow({i:v.encode('utf') for i,v in t.items()})
对此:
writer.writerow(t)
然后,如果可行,您可以将其替换:
Then if that works, you could replace this:
for t in test_text
writer.writerow({i:v.encode('utf') for i,v in t.items()})
使用
writer.writerows(test_text)
这篇关于删除CSV输出中的'b'标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!