![](/img/trans.png)
[英]ERROR: UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 715: character maps to <undefined>
[英]UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 121835: character maps to <undefined>
我正在構建一個基於位置的 Web 應用程序,但我遇到了錯誤。
UnicodeDecodeError:“charmap”編解碼器無法解碼 position 中的字節 0x8d 121835:字符映射到
當我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'
with 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 驗證器驗證您的文件並發布結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.