首页 > 解决方案 > UnicodeDecodeError:“charmap”编解码器无法解码位置 121835 中的字节 0x8d:字符映射到

问题描述

我正在构建一个基于位置的 Web 应用程序,但我遇到了错误。

UnicodeDecodeError:“charmap”编解码器无法解码位置 121835 中的字节 0x8d:字符映射到

migrateDataBase.

模型.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>

我试过什么

当我添加, encoding = 'cp850'thenfilename它显示

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我不知道我做错了什么。

任何帮助,将不胜感激。

先感谢您。

标签: pythondjangoutf-8migration

解决方案


这里有一个语法错误:

shop = apps.get_model('mains','Shop')

但后来你继续:

Shop(name=name,location = location).save()

这是一个关键错误(longitUde):

location = fromstr(f'POINT({longitide} {latitude})', srid=4326)

python 区分大小写。还请为您的文件使用在线 json 验证器并发布结果。


推荐阅读