【问题标题】:Importing CSV from URL and displaying rows on Python by using Requests从 URL 导入 CSV 并使用请求在 Python 上显示行
【发布时间】:2016-08-20 19:33:24
【问题描述】:
import csv
import requests
webpage = requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv')
reader=csv.reader(webpage)
for row in reader:
    print(row)

您好,我是 Python 新手,我正在尝试从 URL 打开一个 CSV 文件,然后显示行,以便从中获取我需要的数据。但是,我收到一条错误消息:

Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 对于阅读器中的行:错误:迭代器应返回字符串,而不是字节(您是否以文本模式打开文件?)

提前谢谢你。

【问题讨论】:

标签: python csv python-requests


【解决方案1】:

Padriac Cunningham 的答案变体使用来自Requestsiter_lines(),并使用列表理解对每一行进行解码

import csv
import requests

webpage = requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv', stream = True)
webpage_decoded = [line.decode('utf-8') for line in webpage.iter_lines()]
reader = csv.reader(webpage_decoded)

或者更简单,您可以让iter_lines() 进行解码

webpage_decoded = webpage.iter_lines(decode_unicode=True)

【讨论】:

    【解决方案2】:

    使用 .text,因为您在 python3 中返回 bytes

    webpage = requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv')
    reader = csv.reader([webpage.text])
    for row in reader:
        print(row)
    

    这给了_csv.Error: new-line character seen in unquoted field,所以在解码后拆分行,stream=True 将允许您不是一次全部获取块中的数据,因此您可以按行过滤并写入:

    import csv
    import requests
    
    
    webpage = requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv', stream=1)
    
    for line in webpage:
         print(list(csv.reader((line.decode("utf-8")).splitlines()))[0])
    

    这给了你:

    ['Day Ahead Hourly LMP Values for 20160427', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['00', '600', '700', '800', '900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300', '2400', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['1', '25.13', '25.03', '28.66', '25.94', '21.74', '19.47', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['600', '600', '600', '700', '700', '700', '800', '800', '800', '900', '900', '900', '1000', '1000', '1000', '1100', '1100', '1100', '1200', '1200', '1200', '1300', '1300', '1300', '1400', '1400', '1400', '1500', '']
    ['1500', '1500', '1600', '1600', '1600', '1700', '1700', '1700', '1800', '1800', '1800', '1900', '1900', '1900']
    ['', '2000', '2000', '2000', '2100', '2100', '2100', '2200', '2200', '2200', '2300', '2300', '2300', '2400', '2400', '2400', '']
    ['lLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'Tot']
    ['alLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'To']
    ['talLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'T']
    .......................................
    

    【讨论】:

    • Thx 但现在我收到一个新错误,即 _csv.Error: new-line character seen in unquoted field - 你需要以通用换行模式打开文件吗?
    • @mostafa14,我添加了几个替代方案
    【解决方案3】:

    你可以试试这个:

    import csv, requests
    webpage=requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv')
    reader = csv.reader(webpage.content.splitlines())
    for row in reader:
      print(row)
    

    希望这会有所帮助

    【讨论】:

    • 感谢您的回答,但我仍然收到相同的错误消息; _csv.Error: 迭代器应该返回字符串,而不是字节(你是否以文本模式打开文件?)
    • 您是否运行了相同的代码?这段代码对我有用。第三行必须是这样的: reader = csv.reader(webpage.content.splitlines())
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多