Reading from CSV: delimiter must be a string, not unicode

后端 未结 2 1012
灰色年华
灰色年华 2020-12-19 04:01

I had a working routine (after a few helpful folks gave me some critical advice in this thread) creating model instances from a CSV file. Up to now I have been using Python

相关标签:
2条回答
  • 2020-12-19 04:27

    Your default encoding is probably not the most appropriate.

    Specify the encoding like this :

    dataReader = csv.reader(open(filename), delimiter=str(u';').encode('utf-8'), quotechar=str(u'"').encode('utf-8'))
    
    0 讨论(0)
  • 2020-12-19 04:38

    Happened to me when I switched the code from a file without from __future__ import unicode_literals to one that had it. (python 2.7)

    It changed the default encoding for string and messed with the existing code.

    Fixed it by changing from:

    # worked before using unicode_literals
    writer = csv.writer(csvfile, delimiter=';', quotechar='"')  
    

    to

    # worked when using unicode_literals
    writer = csv.writer(csvfile, delimiter=str(';'), quotechar=str('"'))  
    
    0 讨论(0)
提交回复
热议问题