![](/img/trans.png)
[英]UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 10752-10753: truncated \uXXXX escape
[英](unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \uXXXX escape
我想导入d:\\ pyusb-1.0.0a2 \\ usb中的pyusb库的一个模块。 首先,我必须将其路径添加到sys.path
。 但我收到以下错误。
注意:我可以成功导入d:\\pyusb-1.0.0a2
!!!
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.path.append('d:\pyusb-1.0.0a2\usb')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \uXXXX escape
您需要使用原始字符串
>>> sys.path.append(r'd:\pyusb-1.0.0a2\usb')
或转义反斜线
>>> sys.path.append('d:\\pyusb-1.0.0a2\\usb')
或者使用正斜杠
>>> sys.path.append('d:/pyusb-1.0.0a2/usb')
否则,Python将尝试将
\\usb\u003c/code>解释为Unicode转义序列(如
\뻯
),由于显而易见的原因而失败。
当我在烧瓶中添加文件上传方法时,我得到了syntaxError,如下所示,
def upload(): if request.method == 'POST': f = request.files['file'] basepath = os.path.dirname(__file__) print(basepath) upload_path = os.path.join(basepath, 'static\files',secure_filename(f.filename)) f.save(upload_path) return redirect(url_for('upload')) return render_template('upload.html')
控制台显示错误,如下所示:
upload_path = os.path.join(basepath,r'static\files',secure_filename(f.filename))
所以,我认为它是由Unicode转义序列中的'\\ u'引起的,我们应该使用原始字符串来修复它。
upload_path = os.path.join(basepath,r'static\files',secure_filename(f.filename))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.