[英]importing csv files in python unicode error unicodeescape
I am trying to work on a csv file in python我正在尝试在 python 中处理 csv 文件
#code experiments
import csv
from statistics import mean
with open("C:\Users\xyr\zzz\kkkk\mmmm\ooooo\mpg.csv",'r') as csvfile:
reader=list(csv.DictReader(csvfile))
for item in reader:
print (item)
l=[item['cty']]
new_list=list(map(float,l))
res=(mean(new_list))
print (res)
gives me the error给我错误
unicodeescape' codec can't decode bytes in position unicodeescape' 编解码器无法解码位置中的字节
However, if I am opening same file in an environment of anaconda's jupyter notebook I am able to open the file.但是,如果我在 anaconda 的 jupyter notebook 环境中打开相同的文件,则可以打开该文件。 This error is coming when I am using python IDLE environment what could be the reason for this error, how can I get rid of these in IDLE environment?当我使用 python IDLE 环境时会出现这个错误,这个错误的原因可能是什么,我怎样才能在 IDLE 环境中摆脱这些? I have tried using single quotes instead of double quotes but still the error remains.我尝试使用单引号而不是双引号,但错误仍然存在。
The main reason you get an error is that \\u is an Unicode escape symbol.出现错误的主要原因是 \\u 是一个 Unicode 转义符号。 if symbols after that are not numeric - it raises an exception如果之后的符号不是数字 - 它会引发异常
Thy adding extra slashes in your path:你在你的路径中添加额外的斜线:
C:\\Users\\xxx\\...
or just use r prefix:或者只使用 r 前缀:
r'C:\Users...'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.