【问题标题】:Can't read a csv file in python无法在python中读取csv文件
【发布时间】:2019-09-24 19:42:35
【问题描述】:

我之前在 python 中打开并读取过很多次 csv 文件,但由于某种原因,这次我一直收到此错误。

错误:

File "C:\Program Files\Python37\lib\encodings\cp1250.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 22: character maps to <undefined>

我的代码:

with open("databa.csv", 'r') as fp:
    for line in fp:
        print(line)

我已经尝试重命名文件,将格式设置为“UTF8”或忽略错误(“databa.csv”、“r”、errors='ignore'),但都没有成功

with open(r"databa.csv", 'rb') as fp: 
with open("databa.csv", 'r', buffering=0) as fp: 
with open("databa.csv", errors="ignore") as fp: 
with open("databa.csv",encoding='utf8', errors="ignore") as fp: 
with open("databa.csv",encoding='utf8', "r") as fp: 
with open("databa.csv", encoding='utf8', "r") as fp

【问题讨论】:

  • I have already tried...setting the format as 'UTF-8' 你能展示一下你尝试了什么吗?
  • 我已经尝试过:with open(r"databa.csv", 'rb') as fp: with open("databa.csv", 'r', buffering=0) as fp:使用 open("databa.csv", errors="ignore") 作为 fp:使用 open("databa.csv",encoding='utf8', errors="ignore") 作为 fp:使用 open("databa.csv" ,encoding='utf8', "r") as fp: with open("databa.csv", encoding='utf8', "r") as fp:
  • 你能粘贴一些csv文件的内容吗?
  • 编辑问题并从评论中添加代码。它会更具可读性。
  • 始终将代码、数据和错误消息置于问题之中。它将更具可读性。你不能在评论中格式化它。

标签: python csv


【解决方案1】:

我运行了您提供的示例,它运行良好,没有任何问题:

提供的样本数据:

输出:

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2019-05-02
    相关资源
    最近更新 更多