python - UnicodeDecodeError:“charmap”编解码器无法解码位置 121835 中的字节 0x8d:字符映射到
问题描述
我正在构建一个基于位置的 Web 应用程序,但我遇到了错误。
UnicodeDecodeError:“charmap”编解码器无法解码位置 121835 中的字节 0x8d:字符映射到
当migrate
我DataBase
.
模型.py
class Shop(models.Model):
name = models.CharField(max_length=100)
location = models.PointField()
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
迁移.py
DATA_FILENAME = 'data.json'
def load_data(apps,schema_editor):
shop = apps.get_model('mains','Shop')
jsonfile = Path(__file__).parents[2] / DATA_FILENAME
with open(str(jsonfile)) as datafile:
objects = json.load(datafile)
for obj in objects['elements']:
try:
objType == obj['type']
if objType == 'node':
tags = obj['tags']
name = tags.get('name','no-name')
longitide = obj.get('lon',0)
latitude = obj.get('lat',0)
location = fromstr(f'POINT({longitide} {latitude})', srid=4326)
Shop(name=name,location = location).save()
except KeyError:
pass
class Migration(migrations.Migration):
dependencies = [
('mains', '0003_shop'),
]
operations = [
migrations.RunPython(load_data)
]
完全错误
Applying mains.0004_auto_20210317_1320...Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\core\management\commands\migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\django\db\migrations\operations\special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "D:\Development Code\myapp\mains\migrations\0004_auto_20210317_1320.py", line 15, in load_data
objects = json.load(datafile)
File "C:\Program Files\Python38\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Program Files\Python38\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 121835: character maps to <undefined>
我试过什么
我尝试了很多答案,例如 put
encoding='cp437
,它对我不起作用。我也试过
encoding="utf8"
了,也没用。
当我添加, encoding = 'cp850'
thenfilename
它显示
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我不知道我做错了什么。
任何帮助,将不胜感激。
先感谢您。
解决方案
这里有一个语法错误:
shop = apps.get_model('mains','Shop')
但后来你继续:
Shop(name=name,location = location).save()
这是一个关键错误(longitUde):
location = fromstr(f'POINT({longitide} {latitude})', srid=4326)
python 区分大小写。还请为您的文件使用在线 json 验证器并发布结果。
推荐阅读
- coldfusion - CFHTTP 无法找到请求目标的有效证书路径
- python - 使用 Keras 进行序列标记 - 忽略前 K 个预测
- javascript - 反应生命周期方法componentDidMount()不起作用
- python - 按多列分组和丢失数据时循环遍历 Pandas 的 groupby 输出
- mongoose - Mongoose - 从其他字段派生 _id
- r - 有没有办法在 R 的轴上从 x 跳到 100?
- c++ - boost_multi_index 迭代器取消引用给出了 const
- android - Flutter 如何接收其他应用的图片
- javascript - 我使用 PEG.js 生成了一个 parser.js 文件,但是在链接到 HTML 时出现导入错误。帮助解决这个问题将不胜感激
- javascript - 如何在网页上显示我保存在 firebase 存储中的图片?(带角度8)