UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 121835: character maps to <undefined>
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 121835: character maps to <undefined>
我正在构建一个基于位置的 Web 应用程序,但遇到错误。
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 121835: character maps to
当我 migrate
DataBase
.
时,此错误一直显示
models.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)
migrations.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[=12=]04_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>
我尝试了什么
我尝试了很多答案,比如 encoding='cp437
,但对我没有用。
我也试了encoding="utf8"
,也没用
当我添加 , encoding = 'cp850'
和 filename
然后它显示
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 验证程序并 post 结果。
我正在构建一个基于位置的 Web 应用程序,但遇到错误。
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 121835: character maps to
当我 migrate
DataBase
.
models.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)
migrations.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[=12=]04_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>
我尝试了什么
我尝试了很多答案,比如
encoding='cp437
,但对我没有用。我也试了
encoding="utf8"
,也没用
当我添加 , encoding = 'cp850'
和 filename
然后它显示
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 验证程序并 post 结果。