Python: URL 数据到 csv reader
Python: URL data to csv reader
我正在尝试将 url 请求的结果转换为 Python 中的 CSV reader object。输出看起来不错,但出现以下错误:
for study_id, sample_id, run_id in reader:
ValueError: not enough values to unpack (expected 3, got 1)
首先我请求数据并将其转换为字符串数据:
req = urllib.request.Request(url=url_get_project_runs, headers={'Content-Type': 'text/plain'})
res = urllib.request.urlopen(req)
dec_res = res.read().decode()
sys.stderr.write(str(dec_res)) --> for the print see below
return dec_res
印刷品将给出:
ERP001736,ERS494374,ERR598958
ERP001736,ERS494394,ERR598963
ERP001736,ERS494431,ERR598964
ERP001736,ERS494445,ERR599170
ERP001736,ERS494488,ERR598996
ERP001736,ERS494518,ERR598976
ERP001736,ERS494559,ERR598986
ERP001736,ERS494579,ERR599078
ERP001736,ERS494616,ERR598944
ERP001736,ERS494628,ERR599001
ERP001736,ERS488919,ERR1701760
这对我来说似乎很好,因为这些项目是用逗号分隔的,并且 "rows" 是完整的。
但是,如果我将其用作 CSV reader 的输入并尝试打印三列,如下所示:
reader = csv.reader(dec_res, delimiter=',')
for study_id, sample_id, run_id in reader:
print(study_id + ", " + sample_id + ", " + run_id)
会出现以下错误:
ValueError: not enough values to unpack (expected 3, got 1)
我进一步测试了一些代码以发现问题:
for row in reader:
sys.stderr.write(str(row))
这将给出:
['E']['R']['P']['0']['0']['1']['7']['3']['6']['', '']['E']['R']['S']['4']['7']...etc
您正在将 str
传递给 csv reader 并且它需要逐行迭代。 str
是逐个字符的可迭代对象。用 StringIO
包裹你的字符串或使用 splitlines
(或任何其他方式提供逐行迭代)。
import csv
from io import StringIO
dec_res = """ERP001736,ERS494374,ERR598958
ERP001736,ERS494394,ERR598963
ERP001736,ERS494431,ERR598964
ERP001736,ERS494445,ERR599170
ERP001736,ERS494488,ERR598996
ERP001736,ERS494518,ERR598976
ERP001736,ERS494559,ERR598986
ERP001736,ERS494579,ERR599078
ERP001736,ERS494616,ERR598944
ERP001736,ERS494628,ERR599001
ERP001736,ERS488919,ERR1701760"""
reader = csv.reader(StringIO(dec_res), delimiter=',')
#reader = csv.reader(dec_res.splitlines(), delimiter=',')
for study_id, sample_id, run_id in reader:
print(study_id + ", " + sample_id + ", " + run_id)
我正在尝试将 url 请求的结果转换为 Python 中的 CSV reader object。输出看起来不错,但出现以下错误:
for study_id, sample_id, run_id in reader:
ValueError: not enough values to unpack (expected 3, got 1)
首先我请求数据并将其转换为字符串数据:
req = urllib.request.Request(url=url_get_project_runs, headers={'Content-Type': 'text/plain'})
res = urllib.request.urlopen(req)
dec_res = res.read().decode()
sys.stderr.write(str(dec_res)) --> for the print see below
return dec_res
印刷品将给出:
ERP001736,ERS494374,ERR598958
ERP001736,ERS494394,ERR598963
ERP001736,ERS494431,ERR598964
ERP001736,ERS494445,ERR599170
ERP001736,ERS494488,ERR598996
ERP001736,ERS494518,ERR598976
ERP001736,ERS494559,ERR598986
ERP001736,ERS494579,ERR599078
ERP001736,ERS494616,ERR598944
ERP001736,ERS494628,ERR599001
ERP001736,ERS488919,ERR1701760
这对我来说似乎很好,因为这些项目是用逗号分隔的,并且 "rows" 是完整的。 但是,如果我将其用作 CSV reader 的输入并尝试打印三列,如下所示:
reader = csv.reader(dec_res, delimiter=',')
for study_id, sample_id, run_id in reader:
print(study_id + ", " + sample_id + ", " + run_id)
会出现以下错误:
ValueError: not enough values to unpack (expected 3, got 1)
我进一步测试了一些代码以发现问题:
for row in reader:
sys.stderr.write(str(row))
这将给出:
['E']['R']['P']['0']['0']['1']['7']['3']['6']['', '']['E']['R']['S']['4']['7']...etc
您正在将 str
传递给 csv reader 并且它需要逐行迭代。 str
是逐个字符的可迭代对象。用 StringIO
包裹你的字符串或使用 splitlines
(或任何其他方式提供逐行迭代)。
import csv
from io import StringIO
dec_res = """ERP001736,ERS494374,ERR598958
ERP001736,ERS494394,ERR598963
ERP001736,ERS494431,ERR598964
ERP001736,ERS494445,ERR599170
ERP001736,ERS494488,ERR598996
ERP001736,ERS494518,ERR598976
ERP001736,ERS494559,ERR598986
ERP001736,ERS494579,ERR599078
ERP001736,ERS494616,ERR598944
ERP001736,ERS494628,ERR599001
ERP001736,ERS488919,ERR1701760"""
reader = csv.reader(StringIO(dec_res), delimiter=',')
#reader = csv.reader(dec_res.splitlines(), delimiter=',')
for study_id, sample_id, run_id in reader:
print(study_id + ", " + sample_id + ", " + run_id)