python - Decoded text file to pandas data frame -
i getting tab separated data file using requests
, want convert pandas data frame. however, can't seem figure out how convert decoded data file pandas data frame object.
import requests import pandas pd datetime import date, timedelta def build_url(site,yesterday): url = 'https://waterdata.usgs.gov/az/nwis/dv?cb_00060=on&format=rdb&site_no=' + gc + '&referred_module=sw&period=&begin_date=1989-01-01&end_date=' + yesterday return url yesterday = date.today() - timedelta(1) yesterday=yesterday.strftime('%y-%m-%d') url = build_url(site,yesterday) t = requests.get(url) decoded = t.content.decode('utf-8') tmp_df = pd.read_csv(decoded,sep='\t',encoding = 'utf8')
my understanding decoded
text file living in memory, when pass pd.read_csv
specified delimiter begins print out data frame , ends with:
usgs 09402500 2017-07-19 15200 p usgs 09402500 2017-07-20 15200 p usgs 09402500 2017-07-21 15100 p usgs 09402500 2017-07-22 15000 p usgs 09402500 2017-07-23 14100 p usgs 09402500 2017-07-24 13700 p not exist
how can pandas convert decoded
dataframe?
read_csv
wants filename or buffer. can either save decoded file, or use stringio
object:
import stringio tmp_df = pd.read_csv(stringio.stringio(decoded), sep='\t')
Comments
Post a Comment