Convert binary input stream to text mode

前端 未结 1 1424
花落未央
花落未央 2021-01-13 23:54

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 =          


        
相关标签:
1条回答
  • 2021-01-14 00:30

    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)
    
    0 讨论(0)
提交回复
热议问题