【问题标题】:Python Reading a Web CSV - UnicodeDecodeError: utf-8 into CSVPython 读取 Web CSV - UnicodeDecodeError: utf-8 into CSV
【发布时间】:2019-05-19 17:58:25
【问题描述】:

访问此 aws CSV 时,我收到以下错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 19: invalid start byte

忽略此错误是可以接受的,考虑到它似乎打印了以下代码(如下)中的所有行。此外,如何将打印的行转换为 pandas 数据框?谢谢。

import requests
from contextlib import closing
import csv

url = "http://google.com/MLI/data/devicefailure2.csv"

with closing(requests.get(url, stream=True)) as r:
    f = (line.decode('utf-8') for line in r.iter_lines())
    reader = csv.reader(f, delimiter=',', quotechar='"')
    for row in reader:
        print(row)

【问题讨论】:

    标签: python-3.x pandas csv python-requests


    【解决方案1】:

    尝试直接读取csv

    pd.read_csv('file', encoding = "ISO-8859-1")
    

    encoding = "utf-8" 会解决问题

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2017-06-28
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多