Taras Danylchenko
Taras Danylchenko

Reputation: 347

From HTTP response to csv

I need to export data from URL to csv, this code works, but data which located on this URL writes in one string without delimiters, how to solve it?

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()

Upvotes: 1

Views: 2973

Answers (1)

MrMister
MrMister

Reputation: 2536

Use this slight variation of your code:

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()

Upvotes: 1

Related Questions