亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 CSV 中的數據迭代 URL 以進行 API 數據拉取 - Python

使用 CSV 中的數據迭代 URL 以進行 API 數據拉取 - Python

呼啦一陣風 2021-09-02 17:23:51
我在一個 CSV 文件的一列中有大約 20,000 個郵政編碼。我正在嘗試通過 API 根據這些郵政編碼提取一些地理選舉數據。此 API 的 url 迭代到最后(每次迭代的郵政編碼都會更改)。我嘗試了許多不同的代碼示例,但都沒有奏效。編輯:在下面粘貼一個我現在知道不起作用的示例 - 主要是因為我不需要條帶功能。但是,如何讓循環直接從 CSV 文件中提取郵政編碼?還粘貼我收到的錯誤消息。    responses = list()    with open("testpostal.csv") as f:        for postal in map(str.strip,f):                 rrr = requests.get('https://represent.opennorth.ca/postcodes/{}'.format(postal))            data = json.loads(rrr.text)            responses.append(data)    print(responses)JSONDecodeError                           Traceback (most recent call last)<ipython-input-182-05e370407e9c> in <module>()      9     for postal in map(str.strip,f):     10         rrr = requests.get('https://represent.opennorth.ca/postcodes/{}'.format(postal))---> 11         data = json.loads(rrr.text)     12         responses.append(data)     13 ~/anaconda3/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)    346             parse_int is None and parse_float is None and    347             parse_constant is None and object_pairs_hook is None and not kw):--> 348         return _default_decoder.decode(s)    349     if cls is None:    350         cls = JSONDecoder~/anaconda3/lib/python3.7/json/decoder.py in decode(self, s, _w)    335     336         """--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())    338         end = _w(s, end).end()    339         if end != len(s):~/anaconda3/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)    353             obj, end = self.scan_once(s, idx)    354         except StopIteration as err:--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None    356         return obj, endJSONDecodeError: Expecting value: line 1 column 1 (char 0)
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

包括您得到的錯誤,這些錯誤將為您指明正確的方向。不過對于初學者來說,

map(str.strip, f)

可能不代表什么。

使用 strip 函數時,您應該包括要“剝離”的內容以及要從中剝離的字符串。例如,從教程:

str = "0000000this is string example....wow!!!0000000";
print str.strip( '0' )

返回:

this is string example....wow!!!


查看完整回答
反對 回復 2021-09-02
  • 3 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號