Unicode 错误 - 用 python 打开 *.txt 文件 [英] Unicode error - opening *.txt files with python

查看:60
本文介绍了Unicode 错误 - 用 python 打开 *.txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 python 中读取这样的文本文件时:

When I try to read a text file like so in python:

x = open("C:\Users\username\Desktop\Hi.txt", 'r')

返回此错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

我环顾四周,发现了这个问题:"Unicode 错误"unicodeescape"编解码器无法解码字节...无法在 Python 3 中打开文本文件.显然我需要复制所有的反斜杠,这样 Unicode 就不会被我想要做的事情搞砸了.所以我做了,但是当我运行 print(x) 时,我得到了这个输出:

I looked around and found this question: "Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3. Apparently I need to duplicate all of the backslashes so Unicode doesn't get all screwed up by what I am trying to do. So I did, but then when I ran print(x) I got this output:

<_io.TextIOWrapper name='C:\\Users\\Sam\\Desktop\\Hi.txt' mode='r' encoding='cp1252'>

这到底是什么,我该如何解决?我正在运行 python 3.3,在 IDLE 中完成所有这些.谢谢.

What on earth is this, and how do I fix it? I am running python 3.3, doing all of this in IDLE. Thanks.

推荐答案

您需要使用带有 Windows 样式文件名的原始字符串:

You need to use raw strings with Windows-style filenames:

x = open(r"C:\Users\username\Desktop\Hi.txt", 'r')
             ^^

否则,Python 的字符串引擎认为 \U 是 Unicode 转义序列的开始 - 当然在这种情况下不是.

Otherwise, Python's string engine thinks that \U is the start of a Unicode escape sequence - which of course it isn't in this case.

那么,你不能像这样简单地print()一个文件,你需要先read()它:

Then, you can't simply print() a file like this, you need to read() it first:

print(x.read())

这篇关于Unicode 错误 - 用 python 打开 *.txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆