简体   繁体   English

在python unicode错误unicodeescape中导入csv文件

[英]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.

相关问题 在Python 3中导入CSV文件时出现unicodeescape错误 - unicodeescape error when importing a CSV File in Python 3 在python中导入csv文件时出现Unicode错误? - Unicode Error when importing a csv file in python? Python脚本生成带有漏洞利用代码的文件-unicodeescape Unicode错误 - Python script generating file with exploit code - unicodeescape unicode error 读取CSV文件unicodeescape时出错 - Error reading csv file unicodeescape python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义错误 - python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义 - Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escap SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position using Selenium Python - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position using Selenium Python 如何修复 python 语法错误 unicodeescape 错误? - How to fix python Syntax error unicodeescape error? SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义错误使用 Selenium 和 Python - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error using Selenium and Python 地址错误:(unicode error)'unicodeescape'编解码器无法解码 - Address error: (unicode error) 'unicodeescape' codec can't decode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM