简体   繁体   English

在Python 3中导入CSV文件时出现unicodeescape错误

[英]unicodeescape error when importing a CSV File in Python 3

It's a very basic question but I've tried many things. 这是一个非常基本的问题,但我已经尝试了很多事情。 My last code is: 我的最后一个代码是:

import csv
with open ('C:\Users\Michel Spiero\Desktop\Base de dados para curso de Python/enrollments.csv') as csvfile:
    readCSV =csv.reader(csvfile, delimiter=',')

    for row in readCSV:
        print(row)

I am getting this error: 我收到此错误:

File "<ipython-input-9-3103e7dc9e55>", line 3
    with open ('C:\Users\Michel Spiero\Desktop\Base de dados para curso de Python/enrollments.csv') as csvfile:
              ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

What Should I do? 我该怎么办?

Thanks 谢谢

Backslash U ( \\U ) has a special meaning in string literals. 反斜杠U( \\U )在字符串文字中具有特殊含义。 String and Bytes literals in the documentation says for \\Uxxxxxxxx the meaning is „Character with 32-bit hex value xxxxxxxx“ . 文档中的字符串和字节字面量表示\\Uxxxxxxxx的含义是“具有32位十六进制值xxxxxxxx的字符”

So you have to escape at least the backslash before the U of Users, or put an r in front of the string so no backslash has a special meaning. 因此,您必须至少在用户U之前转义反斜杠,或者在字符串前面放置r ,以便没有反斜杠具有特殊含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM