Writing to io.BytesIO in csv fails in python3

后端 未结 1 405
礼貌的吻别
礼貌的吻别 2020-12-15 06:07

I am trying to write python 2/3 compatible code to write strings to csv file object. This code:

line_as_list = [line.encode() for line in line_as_list]
write         


        
相关标签:
1条回答
  • 2020-12-15 06:56

    In Python3 csv.writer expects a file-like object opened in text mode. In Python2, csv.writer expects a file-like object opened in binary mode.

    Therefore, in Python3, use io.StringIO, while in Python2 use io.BytesIO:

    import io
    import csv
    import sys
    PY3 = sys.version_info[0] == 3
    
    line_as_list = [u'foo', u'bar']
    encoding = 'utf-8'
    
    if PY3:
        writer_file =  io.StringIO()
    else:
        writer_file =  io.BytesIO()
        line_as_list = [line.encode(encoding) for line in line_as_list]
    
    writer = csv.writer(writer_file, dialect='excel', delimiter=',')
    writer.writerow(line_as_list)
    content = writer_file.getvalue()
    
    if PY3:
        content = content.encode(encoding)
    
    print(type(content))
    print(repr(content))
    

    In Python3 the code above prints

    <class 'bytes'>
    b'foo,bar\r\n'
    

    In Python2 the code above prints

    <type 'str'>
    'foo,bar\r\n'
    
    0 讨论(0)
提交回复
热议问题