将二进制输入流转换为文本模式 [英] Convert binary input stream to text mode
问题描述
datafile = open('./ file.csv',mode ='rt' )
data = csv.reader(datafile)
for数据:#works
process(e)
问题在于, BZ2File
只支持创建二进制流,而在Python 3中, csv.reader
只接受文字流。 (同样的问题发生在gzip和zip文件中。)
$ p $ datafile = bz2.BZ2File('./ file.csv.bz2 ',mode ='r')
data = csv.reader(datafile)
表示数据中的e:#error
进程(e)
特别是,指定的行会抛出异常 _csv.Error:迭代器应该返回字符串,而不是字节(你打开文件在文本模式?)
。
我也试过 data = csv.reader(codecs.EncodedFile(datafile ,'utf8'))
,但是这并不能解决错误。
如何包装二进制输入流在文本模式下使用
< pre $ import codecs,csv
f = codecs.open(file.csv,r,utf-8)
g = csv.reader(f )b
print(e)
BZ2:
导入编解码器,csv,bz2
f = bz2.BZ2File (./file.csv.bz2,mode =r)
c = codecs.iterdecode(f,utf-8)
g = csv.reader(c)
例如:
print(e)
I am trying to read a bzip2-compressed CSV file in Python 3.2. For an uncompressed CSV file, this works:
datafile = open('./file.csv', mode='rt')
data = csv.reader(datafile)
for e in data: # works
process(e)
The problem is that BZ2File
only supports creating a binary stream, and in Python 3, csv.reader
accepts only text streams. (The same issue occurs with gzip and zip files.)
datafile = bz2.BZ2File('./file.csv.bz2', mode='r')
data = csv.reader(datafile)
for e in data: # error
process(e)
In particular, the indicated line throws the exception _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
.
I've also tried data = csv.reader(codecs.EncodedFile(datafile, 'utf8'))
, but that doesn't fix the error.
How can I wrap the binary input stream so that it can be used in text mode??
This works for me:
import codecs, csv
f = codecs.open("file.csv", "r", "utf-8")
g = csv.reader(f)
for e in g:
print(e)
In the case of BZ2:
import codecs, csv, bz2
f = bz2.BZ2File("./file.csv.bz2", mode="r")
c = codecs.iterdecode(f, "utf-8")
g = csv.reader(c)
for e in g:
print(e)
这篇关于将二进制输入流转换为文本模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!