‘utf-8’ не может декодировать байт 0xc0 в позиции 0: недопустимый начальный байт

#python #django

#python #django

Вопрос:

Я пытаюсь выполнить createsuperuser от Django и получаю следующую ошибку. Ранее база данных syncdb была пуста и закодирована в ‘utf-8’. Я использую pymysql.

 D:Python27sitemysite>python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "D:python27libsite-packagesdjangocoremanagement__init__.py", line
438, in execute_manager
    utility.execute()
  File "D:python27libsite-packagesdjangocoremanagement__init__.py", line
379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:python27libsite-packagesdjangocoremanagementbase.py", line 191,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "D:python27libsite-packagesdjangocoremanagementbase.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "D:python27libsite-packagesdjangocoremanagementbase.py", line 351,
 in handle
    return self.handle_noargs(**options)
  File "D:python27libsite-packagesdjangocoremanagementcommandssyncdb.py"
, line 109, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "D:python27libsite-packagesdjangocoremanagementsql.py", line 190,
in emit_post_sync_signal
    interactive=interactive, db=db)
  File "D:python27libsite-packagesdjangodispatchdispatcher.py", line 172,
in send
    response = receiver(signal=self, sender=sender, **named)
  File "D:python27libsite-packagesdjangocontribauthmanagement__init__.py
", line 70, in create_superuser
    call_command("createsuperuser", interactive=True)
  File "D:python27libsite-packagesdjangocoremanagement__init__.py", line
166, in call_command
    return klass.execute(*args, **defaults)
  File "D:python27libsite-packagesdjangocoremanagementbase.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "D:python27libsite-packagesdjangocontribauthmanagementcommandscr
eatesuperuser.py", line 72, in handle
    User.objects.get(username=default_username)
  File "D:python27libsite-packagesdjangodbmodelsmanager.py", line 132, in
 get
    return self.get_query_set().get(*args, **kwargs)
  File "D:python27libsite-packagesdjangodbmodelsquery.py", line 344, in g
et
    num = len(clone)
  File "D:python27libsite-packagesdjangodbmodelsquery.py", line 82, in __
len__
    self._result_cache = list(self.iterator())
  File "D:python27libsite-packagesdjangodbmodelsquery.py", line 273, in i
terator
    for row in compiler.results_iter():
  File "D:python27libsite-packagesdjangodbmodelssqlcompiler.py", line 68
0, in results_iter
    for rows in self.execute_sql(MULTI):
  File "D:python27libsite-packagesdjangodbmodelssqlcompiler.py", line 73
5, in execute_sql
    cursor.execute(sql, params)
  File "D:python27libsite-packagesdjangodbbackendsutil.py", line 34, in e
xecute
    return self.cursor.execute(sql, params)
  File "D:python27libsite-packagesdjangodbbackendsmysqlbase.py", line 86
, in execute
    return self.cursor.execute(query, args)
  File "buildbdist.win32eggpymysqlcursors.py", line 96, in execute
  File "buildbdist.win32eggpymysqlconnections.py", line 575, in escape
  File "buildbdist.win32eggpymysqlconverters.py", line 22, in escape_item
  File "buildbdist.win32eggpymysqlconverters.py", line 45, in escape_sequenc
e
  File "buildbdist.win32eggpymysqlconverters.py", line 27, in escape_item
  File "D:python27libencodingsutf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc0 in position 0: invalid s
tart byte
  

Ответ №1:

Байт с кодом C0 недопустим в тексте, закодированном в UTF-8. Я не знаю, откуда взят этот текст, но это либо недопустимый UTF-8, либо ваш код ошибочно пытается обработать его как UTF-8.