python - 从 HTTP 响应到 csv

标签 python python-3.x python-2.7 python-requests

我需要将数据从 URL 导出到 csv,这段代码可以工作,但是位于该 URL 上的数据写入一个字符串中,没有分隔符,如何解决?

import urllib
import pandas as pd

URL="https://www.star.nesdis.noaa.gov/smcd/emb/vci/NPP_VH/4km/ts_L1_v1/ByWeek/VHP_L1_Mean_198135.txt"
vhi_url = urllib.request.urlopen(URL)
out = open('vhi_id_16.csv','wb')
out.write(vhi_url.read())
out.close()

最佳答案

使用代码的这个细微变化:

from urllib import request

with open('vhi_id_16.csv', 'w') as out:
    with request.urlopen('PUT_URL_HERE') as response:
        encoding = response.headers.get_content_charset('utf-8')
        out.write(response.read().decode(encoding))
        out.close()

关于python - 从 HTTP 响应到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282794/

相关文章:

python - 如何将 @tf.function 与 Keras 顺序 API 一起使用?

python - 在Python中逆时针方向对多边形坐标列表进行排序

python - 查找由已知字符串分隔的表达式

python - 与功能参数作斗争

python-3.x - 使用嵌套圆圈作为图例

python - 如何将 travis ci 与 Python 中的 codeclimate 测试覆盖率集成?

python - python2/3中input()的区别

python - 有人可以解释 Gtk2 包装吗?

python - 无法从类传递调整大小的 QLabel 几何

python - WindowsError : [Error 2] The system cannot find the file specified, 无法在 Python 中解析