Python (unicode 错误)“unicodeescape”编解码器无法解码位置 2-3 中的字节:截断的 \UXXXXXXXX 转义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37400974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
提问by Miesje
I'm trying to read a .csv file into Python (Spyder) but I keep getting an error. My code:
我正在尝试将 .csv 文件读入 Python (Spyder),但我一直收到错误消息。我的代码:
import csv
data = open("C:\Users\miche\Documents\school\jaar2\MIK.6\vektis_agb_zorgverlener")
data = csv.reader(data)
print(data)
I get the following error:
我收到以下错误:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 2-3 中的字节:截断的 \UXXXXXXXX 转义
I have tried to replace the \ with \ or with / and I've tried to put an r before "C..but all these things didn't work.
我试图用 \ 或用 / 替换 \ 并且我试图在“C ..”之前放一个 r但所有这些都不起作用。
回答by DeePak M. Birajdar
This error occurs because you are using a normal string as a path. You can use one of the following solutions to fix your problem.
出现此错误是因为您使用的是普通字符串作为路径。您可以使用以下解决方案之一来解决您的问题。
- Just put
r
before your normal string it converts normal string to raw string:
- 放在
r
普通字符串之前,它将普通字符串转换为原始字符串:
pandas.read_csv(r"C:\Users\DeePak\Desktop\myac.csv")
pandas.read_csv(r"C:\Users\DeePak\Desktop\myac.csv")
2:
2:
pandas.read_csv("C:/Users/DeePak/Desktop/myac.csv")
3:
3:
pandas.read_csv("C:\Users\DeePak\Desktop\myac.csv")
回答by thomasrutter
The first backslash in your string is being interpreted as a special character, in fact because it's followed by a "U" it's being interpreted as the start of a unicode code point.
字符串中的第一个反斜杠被解释为特殊字符,实际上因为它后面跟着一个“U”,它被解释为 unicode 代码点的开始。
To fix this you need to escape the backslashes in the string. I don't know Python specifically but I'd guess you do it by doubling the backslashes:
要解决此问题,您需要转义字符串中的反斜杠。我不特别了解 Python,但我猜你可以通过加倍反斜杠来做到这一点:
data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
回答by Ramineni Ravi Teja
consider it as a raw string. Just simple answer, add r
before your windows path.
将其视为原始字符串。只是简单的答案,r
在您的 Windows 路径之前添加。
import csv data = open(r"C:\Users\miche\Documents\school\jaar2\MIK.6\vektis_agb_zorgverlener") data = csv.reader(data) print(data)
import csv data = open(r"C:\Users\miche\Documents\school\jaar2\MIK.6\vektis_agb_zorgverlener") data = csv.reader(data) print(data)
回答by Mohit Solanki
You can just put r
in front ofthe string with your actual path, which denotes a raw string. For example:
您可以将实际路径放在字符串r
前面,这表示原始字符串。例如:
data = open(r"C:\Users\miche\Documents\school\jaar2\MIK.6\vektis_agb_zorgverlener")
回答by Ibrahim Isa
Try writing the file path as "C:\\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener"
i.e with double backslash after the drive as opposed to "C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener"
尝试"C:\\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener"
在驱动器后使用双反斜杠写入文件路径,而不是"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener"
回答by DebanjanB
As per String literals:
根据字符串文字:
String literals can be enclosed within single quotes(i.e.
'...'
) or double quotes(i.e."..."
). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings).The backslash character (i.e.
\
) is used to escape characters which otherwise will have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letterr
orR
. Such strings are called raw stringsand use different rules for backslash escape sequences.In triple-quoted strings, unescaped newlinesand quotesare allowed, except that the three unescaped quotesin a row terminate the string.
Unless an
r
orR
prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.
字符串文字可以用单引号(即
'...'
)或双引号(即"..."
)括起来。它们也可以包含在由三个单引号或双引号组成的匹配组中(这些通常称为三引号字符串)。反斜杠字符(即
\
)用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符。字符串文字可以选择性地以字母r
或为前缀R
。此类字符串称为原始字符串,对反斜杠转义序列使用不同的规则。在三重引号字符串中,允许使用未转义的换行符和引号,但一行中的三个未转义的引号会终止字符串。
除非存在
r
orR
前缀,否则字符串中的转义序列将根据与标准 C 使用的规则类似的规则进行解释。
So ideally you need to replace the line:
所以理想情况下,您需要替换该行:
data = open("C:\Users\miche\Documents\school\jaar2\MIK.6\vektis_agb_zorgverlener")
To any one of the following characters:
对以下任一字符:
Using rawprefix and single quotes(i.e.
'...'
):data = open(r'C:\Users\miche\Documents\school\jaar2\MIK.6\vektis_agb_zorgverlener')
Using double quotes(i.e.
"..."
) and escaping backslash character (i.e.\
):data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
Using double quotes(i.e.
"..."
) and forwardslash character (i.e./
):data = open("C:/Users/miche/Documents/school/jaar2/MIK/2.6/vektis_agb_zorgverlener")
使用原始前缀和单引号(即
'...'
):data = open(r'C:\Users\miche\Documents\school\jaar2\MIK.6\vektis_agb_zorgverlener')
使用双引号(即
"..."
)和转义反斜杠字符(即\
):data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
使用双引号(即
"..."
)和正斜杠字符(即/
):data = open("C:/Users/miche/Documents/school/jaar2/MIK/2.6/vektis_agb_zorgverlener")
回答by Subhashi
Just putting an r in front works well.
只在前面放一个 r 效果很好。
eg:
例如:
white = pd.read_csv(r"C:\Users\hydro\a.csv")
回答by farshad
put r
before your string, it converts normal string to raw string
放在r
您的字符串之前,它将普通字符串转换为原始字符串
回答by vinod
it worked for me by neutralizing the '\' by f = open('F:\\file.csv')
它通过 f = open('F:\\file.csv') 中和 '\' 对我有用
回答by G4W
The double \ should work for Windows, but you still need to take care of the folders you mention in your path. All of them (exept the filename) must exist. otherwise you will get an error.
双 \ 应该适用于 Windows,但您仍然需要注意您在路径中提到的文件夹。所有这些(除了文件名)都必须存在。否则你会得到一个错误。