【发布时间】:2018-11-23 18:57:00
【问题描述】:
我正在尝试将数据从 csv 文件导入 Django 模型。我正在使用 manage.py shell 和以下代码:
>>> import csv
>>> import os
>>> path = "C:\\Users\Lia Love\Downloads"
>>> os.chdir(path)
>>> from catalog.models import ProductosBase
>>> with open('FarmaciasGob.csv') as csvfile:
... reader = csv.DictReader(csvfile)
... for row in reader:
... p = Country(country=row['Country'], continent=row['Continent'])
... p.save()
...
>>>
>>> exit()
我在数据集的给定点收到以下错误消息:
UnicodeDecodeError: "charmap" codec can´t decode byte 0x81 in position 7823: character maps to (undefined)
据我所知,这似乎是 csv 文件的“拉丁”编码存在问题。
检查 csv,我看不出错误所在的特定行没有什么特别之处。我可以在此之前导入大约 2200 行,全部带有拉丁字符。
有什么线索吗?
【问题讨论】:
-
这是编码的问题,你用的是python2还是python3?
-
使用 Python 3.7.0