我正在尝试使用Black来格式化我的Django代码。它在大多数目录中都工作得很好。但是,我得到Unicode解码错误UnicodeDecodeError:'utf-8‘编解码器无法解码位置0的字节0xff :特别是在一个目录中的.py文件上无效的起始字节。这些文件是UTF-8格式的,但我尝试了多种不同的其他编码,并尝试删除和重新创建文件,但都没有解决这个问题。我在Anaconda环境中工作,但是停用它并没有什么不同。我还禁用了我的大多数VS代码扩展。
我已经看了很多关于这方面的帖子,但到目前为止没有一个建议有帮助。我在苹果电脑上使用VS,有一种感觉,那是因为在过去,我曾在Windows机上使用VS with WSL (与GitHub代码库同步的代码)编辑相同的代码,但不知道如何解决这个问题。我在下面放了一个示例回溯调用。任何帮助都将不胜感激!
编辑:如果我剪切任何文件并将其粘贴到另一个目录中,则Black可以正常工作,并且我不会收到任何编码错误消息。这表明可能存在目录级问题?
(base) ➜ healthtic git:(master) ✗ black pages/views.py
Traceback (most recent call last):
File "/Users/jh/opt/anaconda3/bin/black", line 8, in <module>
sys.exit(patched_main())
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/black/__init__.py", line 1130, in patched_main
main()
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/black/__init__.py", line 411, in main
sources = get_sources(
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/black/__init__.py", line 477, in get_sources
gitignore = get_gitignore(root)
File "/Users/jh/opt/anaconda3/lib/python3.8/site-packages/black/files.py", line 123, in get_gitignore
lines = gf.readlines()
File "/Users/jh/opt/anaconda3/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
进一步编辑:如上所述,如果我将文件移出此目录,Black将正常工作,如果我将它们移回(没有任何更改),则0xFF unicode错误再次出现。我在下面附上了我的目录结构,因为它可能与我在其中的一些前端的webpack和节点模块相关。我的印象是,也许在目录中以某种方式添加了BOM?
├── Dockerfile
├── accounts
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── appointments
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── cloud_sql_proxy
├── cloudmigrate.yaml
├── config
│ ├── __init__.py
│ ├── __pycache__
│ ├── asgi.py
│ ├── basesettings.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── db.sqlite3
├── docker-compose.yaml
├── front-end
│ ├── App\ copy.js
│ ├── App.js
│ ├── index\ copy.js
│ └── index.js
├── logfile
├── manage.py
├── node_modules
│ ├── @babel
│ ├── @discoveryjs
│ ├── @types
│ ├── @webassemblyjs
│ ├── @webpack-cli
│ ├── @xtuc
│ ├── acorn
│ ├── ajv
│ ├── ajv-keywords
│ ├── ansi-styles
│ ├── babel-loader
│ ├── babel-plugin-dynamic-import-node
│ ├── babel-plugin-polyfill-corejs2
│ ├── babel-plugin-polyfill-corejs3
│ ├── babel-plugin-polyfill-regenerator
│ ├── big.js
│ ├── browserslist
│ ├── buffer-from
│ ├── call-bind
│ ├── caniuse-lite
│ ├── chalk
│ ├── chrome-trace-event
│ ├── clone-deep
│ ├── color-convert
│ ├── color-name
│ ├── colorette
│ ├── commander
│ ├── commondir
│ ├── convert-source-map
│ ├── core-js-compat
│ ├── cross-spawn
│ ├── debug
│ ├── define-properties
│ ├── electron-to-chromium
│ ├── emojis-list
│ ├── enhanced-resolve
│ ├── envinfo
│ ├── es-module-lexer
│ ├── escalade
│ ├── escape-string-regexp
│ ├── eslint-scope
│ ├── esrecurse
│ ├── estraverse
│ ├── esutils
│ ├── events
│ ├── execa
│ ├── fast-deep-equal
│ ├── fast-json-stable-stringify
│ ├── fastest-levenshtein
│ ├── find-cache-dir
│ ├── find-up
│ ├── function-bind
│ ├── gensync
│ ├── get-intrinsic
│ ├── get-stream
│ ├── glob-to-regexp
│ ├── globals
│ ├── graceful-fs
│ ├── has
│ ├── has-flag
│ ├── has-symbols
│ ├── human-signals
│ ├── import-local
│ ├── interpret
│ ├── is-core-module
│ ├── is-plain-object
│ ├── is-stream
│ ├── isexe
│ ├── isobject
│ ├── jest-worker
│ ├── js-tokens
│ ├── jsesc
│ ├── json-parse-better-errors
│ ├── json-schema-traverse
│ ├── json5
│ ├── kind-of
│ ├── loader-runner
│ ├── loader-utils
│ ├── locate-path
│ ├── lodash
│ ├── lodash.debounce
│ ├── loose-envify
│ ├── make-dir
│ ├── merge-stream
│ ├── mime-db
│ ├── mime-types
│ ├── mimic-fn
│ ├── minimist
│ ├── ms
│ ├── neo-async
│ ├── node-releases
│ ├── npm-run-path
│ ├── object-assign
│ ├── object-keys
│ ├── object.assign
│ ├── onetime
│ ├── p-limit
│ ├── p-locate
│ ├── p-try
│ ├── path-exists
│ ├── path-key
│ ├── path-parse
│ ├── pkg-dir
│ ├── punycode
│ ├── randombytes
│ ├── react
│ ├── react-dom
│ ├── rechoir
│ ├── regenerate
│ ├── regenerate-unicode-properties
│ ├── regenerator-runtime
│ ├── regenerator-transform
│ ├── regexpu-core
│ ├── regjsgen
│ ├── regjsparser
│ ├── resolve
│ ├── resolve-cwd
│ ├── resolve-from
│ ├── safe-buffer
│ ├── scheduler
│ ├── schema-utils
│ ├── semver
│ ├── serialize-javascript
│ ├── shallow-clone
│ ├── shebang-command
│ ├── shebang-regex
│ ├── signal-exit
│ ├── source-list-map
│ ├── source-map
│ ├── source-map-support
│ ├── strip-final-newline
│ ├── supports-color
│ ├── tapable
│ ├── terser
│ ├── terser-webpack-plugin
│ ├── to-fast-properties
│ ├── unicode-canonical-property-names-ecmascript
│ ├── unicode-match-property-ecmascript
│ ├── unicode-match-property-value-ecmascript
│ ├── unicode-property-aliases-ecmascript
│ ├── uri-js
│ ├── v8-compile-cache
│ ├── watchpack
│ ├── webpack
│ ├── webpack-cli
│ ├── webpack-merge
│ ├── webpack-sources
│ ├── which
│ ├── wildcard
│ └── yocto-queue
├── notes.md
├── package-lock.json
├── package.json
├── pages
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── patient
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── patientinfo
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── readme.md
├── requirements.txt
├── static
│ ├── css
│ ├── images
│ ├── index-bundle.js
│ ├── index-bundle.js.LICENSE.txt
│ └── js-bundle.js
├── staticfiles
│ ├── admin
│ ├── css
│ └── images
├── templates
│ ├── _base.html
│ ├── account
│ ├── appointments
│ ├── frontend.html
│ ├── hello_webpack.html
│ ├── home.html
│ ├── patient
│ └── patientinfo
├── todos.md
└── webpack.config.js
发布于 2021-07-25 22:14:33
我甚至不知道django是什么,但我在非django程序中遇到了同样的问题。该代码片段中的问题现已修复:
open(document.txt, mode= "r", encoding="utf8")
如你所见,它的编码是utf8,没有"-“。我使用该代码打开文档并阅读它。我希望这将是有用的!
https://stackoverflow.com/questions/68519370
复制相似问题