我试图将CSV文件读入Python (Spyder),但我一直得到一个错误。我的代码:

import csv

data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
data = csv.reader(data)
print(data)

我得到以下错误:

SyntaxError (unicode错误)“unicodesescape”编解码器不能解码字节 在位置2-3:截断\UXXXXXXXX转义

我试过把\换成\\或者/,还试过在“C”前面加一个r。,但所有这些都不起作用。


当前回答

根据字符串字面量:

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 letter r or R. Such strings are called raw strings and use different rules for backslash escape sequences. In triple-quoted strings, unescaped newlines and quotes are allowed, except that the three unescaped quotes in a row terminate the string. Unless an r or R prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.

所以理想情况下你需要替换这一行:

data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")

致下列任何一个字符:

使用原始前缀和单引号(即“…”): data = open(r' 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") 使用双引号(即"…")和正斜杠字符(即/): data = open("C:/Users/miche/Documents/school/jaar2/MIK/2.6/vektis_agb_zorgverlener")

其他回答

你可以把r放在你实际路径的字符串前面,它表示一个原始字符串。例如:

data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")

它通过中和“by f = open(' f:\\file.csv')”来为我工作。

双\应该适用于Windows,但你仍然需要照顾你在路径中提到的文件夹。它们(除了文件名)都必须存在。否则您将得到一个错误。

尝试将文件路径写为“C:\\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener”,即在驱动器后加双反斜杠,而不是“C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener”

根据字符串字面量:

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 letter r or R. Such strings are called raw strings and use different rules for backslash escape sequences. In triple-quoted strings, unescaped newlines and quotes are allowed, except that the three unescaped quotes in a row terminate the string. Unless an r or R prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.

所以理想情况下你需要替换这一行:

data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")

致下列任何一个字符:

使用原始前缀和单引号(即“…”): data = open(r' 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") 使用双引号(即"…")和正斜杠字符(即/): data = open("C:/Users/miche/Documents/school/jaar2/MIK/2.6/vektis_agb_zorgverlener")