Django CSV file Error: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb5 in position 0: invalid start

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb5 in position 0: invalid start

This csv file I opened in jupyter using pd.read_csv open no problem

But after I cleaned the data and saved it as a new csv, I couldn’t open it and got an error

Solution: in the views.py direct cleaning work, the data will be stored in order into a list, using a loop to iterate through the database, the following is the code (less efficient after all is iterative. Welcome to share a better way)

df = pd.read_csv(r"xxxxx\xxx.csv", encoding='utf-8')
... # datas clean up
ls = []
for index, row in df.iterrows():
     res = []
     for i in df:
         res.append(row[i])
            ls.append(res)
for i in range(len(ls)):
    try:
        XXX.objects.create(title=ls[i][0], rating=ls[i][1])
    except Exception as e:
        print(e)
return HttpResponse('Datas save successfully')

Read More: