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.
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...
- [Django]-Get ID of Django record after it has been created in a ModelViewSet
- [Django]-Recursive relationship in django doesn't work
- [Django]-How to use has_object_permission with APIView in Django Rest Framework?
- [Django]-Framework/Language for new web 2.0 sites (2008 and 2009)
- [Django]-MemoryError with django
-2👍
Check with the header line in csv file.It should be exact same as the column name.
- [Django]-Keep User and Group in same section in Django admin panel
- [Django]-Using HttpResponseRedirect, but browser is not showing correct URL
- [Django]-Django app not found despite adding to INSTALLED_APPS?
Source:stackexchange.com