[Django]-New-line character seen in unquoted field

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.

3👍

You can change the read mode to ‘rU’ instead of ‘r’ or ‘rb’

1👍

This is how I avoided the above error while reading from the uploaded CSV file that contains some special symbols also.

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...
👤vijay

-2👍

Check with the header line in csv file.It should be exact same as the column name.

Leave a comment