python - CSV 以字节形式返回而不是字符串 Python

标签 python python-3.x urllib pymysql

我收到的错误是这样的:

_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

我可以在代码中的哪个位置以文本模式打开文件?

import csv
import urllib.request
import pymysql.cursors
connection = pymysql.connect(host='localhost',
                         user='',
                         password='',
                         db='mydb',
                         charset='utf8',
                         cursorclass=pymysql.cursors.DictCursor)
try:
    url = 'https://api.iextrading.com/1.0/stock/market/collection/sector?
    collectionName=Health%20Care&format=csv'
    response =  urllib.request.urlopen(url)
    csv_data = csv.reader(response)
    for row in csv_data:

    cursor.execute('INSERT INTO Financials (names, \
          classes, mark )' \
          'VALUES("%s", "%s", "%s")', 
          row)
finally:
    connection.close()

最佳答案

response =  urllib.request.urlopen(url)

此处response返回bytes对象上的迭代器。

如果您知道 csv 文件只是纯文本,则可以插入生成器理解来解码行:

csv_data = csv.reader(line.decode() for line in response)

map (当你可以在没有 lambda 的情况下使用 python 3 map 时,你难道不喜欢它吗?)

csv_data = csv.reader(map(bytes.decode,response))

独立示例:

import urllib.request,csv

url = 'https://api.iextrading.com/1.0/stock/market/collection/sector?collectionName=Health%20Care&format=csv'
response =  urllib.request.urlopen(url)
for row in csv.reader(line.decode() for line in response):
    print(row)

现在您正在向 csv.reader 提供字符串列表,这将起作用。

示例输出:

['CAPR', 'Capricor Therapeutics Inc.', 'NASDAQ Capital Market', 'Healthcare', 'close', '0.9011', '1539955800519', '0.875', '1539979200341', '0.9011', '0.8702', '0.875', 'Close', 'October 19, 2018', '1539979200341', '96625', '', '', '', '0.875', '1539979200341', '0.9011', '0.0261', '0.02983', '1539954158310', '0.9011', '-0.0261', '-0.02896', '', '', '106532', '', '', '', '', '26905263', '-1.72', '3.19', '0.851', '-0.4716351592356688']
['AVDL', 'Avadel Pharmaceuticals plc', 'NASDAQ Global Market', 'Healthcare', 'close', '4.24', '1539955800643', '4.1', '1539979200312', '4.24', '4', '4.1', 'Close', 'October 19, 2018', '1539979200312', '78386', '', '', '', '4.1', '1539979200312', '4.2', '0.1', '0.02439', '1539892803066', '4.2', '-0.1', '-0.02381', '', '', '114387', '', '', '', '', '150734712', '-3.8', '11.93', '3.98', '-0.5579009090909092']

关于python - CSV 以字节形式返回而不是字符串 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52903940/

相关文章:

python - 没有名为 livelossplot 的模块

python - 理论上,对于 Apache Spark,Scala 比 Python 更快。实际上并非如此。这是怎么回事?

Python 3 网站在使用用户代理欺骗时检测抓取工具

python - 从 FastAPI 中的在线视频 URL 返回文件/流响应

python - 请求异常.MissingSchema : Invalid URL Python API Get request

python - 更新 numpy 数组的 3 维和 4 维元素

python - 将这些对象转换为 python 列中的 int64

python - 值错误: could not convert string to float: '' in Python 3

python - 使用 matplotlib 的结果不一致

python - 没有名为 pkg_resources.py2_warn pyinstaller 的模块