2.7 CSV module wants unicode, but doesn't want unicode

后端 未结 3 1641
余生分开走
余生分开走 2021-02-19 08:01
csvfile_ = open(finishedFileName+num+\".csv\",\"w\",newline=\'\')
writ = csv.writer(csvfile_, dialect=\'excel\')
firstline = unicode(str(firstline))
try:
    writ.writer         


        
3条回答
  •  我寻月下人不归
    2021-02-19 08:20

    Martijn Pieters' solution using 'w' or 'wb' does not seem to work because of the newline argument. I personally get a ValueError.

    ValueError: binary mode doesn't take a newline argument
    

    Which I don't really understand, I would expect io to ignore it rather than raise an Exception. The only solution that works for me both on python 2 and 3 is:

    if sys.version_info.major < 3:
        open(my_csv_file, 'rb')
    else:
        open(my_csv_file, 'r', newline='')
    

    Solution that can become very heavy when you open a lot of files. Martijn solution was cleaner in that regard, if only it could work!

    EDIT: I think the cleanest working solution when developing a package that needs to read/write files often is to create a small utility function that can be called everywhere in the package:

    import sys
    import io
    
    def open_csv_rb(my_file):
        if sys.version_info[0] < 3:
            return io.open(my_file, 'rb')
        else:
            return io.open(my_file, 'r', encoding='utf8')
    
    def open_csv_wb(my_file):
        if sys.version_info[0] < 3:
            return io.open(my_file, 'wb')
        else:
            return io.open(my_file, 'w', newline='', encoding='utf8')
    

提交回复
热议问题