3

I have a file loaded in memory from a Django form. It is giving me the following error:

new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

Another solution used this.

However, i already have the file in memory so i require to change it to opening in "rU" format.

This is my relevant code in views.py

form = ResultsUploadForm(request.POST, request.FILES)
    if form.is_valid():
        form.save()
        reader = csv.reader(request.FILES['file'])
        for row in reader:
            #etc...

Does anyone know how to go about fixing this? The error is thrown from this statement for row in reader:

4

4 回答 4

7

I found the solution in another post

The issue was how i was saving the .csv file. When producing a .csv file in excel for mac save it as "Windows Comma Separated Values (.csv)" This will stop the addition of unwanted characters that throw of import csv in Django and python.

于 2013-07-21T09:16:24.277 回答
3

您可以将读取模式更改为“rU”而不是“r”或“rb”

于 2016-08-06T17:33:11.943 回答
1

这就是我在读取上传的包含一些特殊符号的 CSV 文件时避免上述错误的方法。

def utf_8_encoder(unicode_csv_data): //encodes the Unicode strings as UTF-8 
    for line in unicode_csv_data:
        yield line.encode('utf-8')


def validate_csv(request):
    csv_contents = request.FILES['files'].read().decode('utf-8-sig') // to avoid csv exception while reading unwanted characters (eg: \xef\xbb\xbf)
    request_file = csv_contents.splitlines()
    dict_reader = csv.DictReader(utf_8_encoder(request_file)) // avoid error - ascii' codec can't encode character u'\\ufeff' in position 0: ordinal not in range(128)
    #because data contain some special symobls like G�mez
    fieldnames = dict_reader.fieldnames //fieldnames contain column header
    for item in dict_reader:
        #etc...
于 2017-02-23T09:56:37.383 回答
-2

检查 csv 文件中的标题行。它应该与列名完全相同。

于 2014-06-23T16:35:46.930 回答