Basic Human Being
Basic Human Being

Reputation: 93

Python 2.7 csv download from URL

I'm trying to do some basic analsys on ether historical prices for a school project. My problem is quite a simple one I think. I made a function that download the data from the URL, but the format is wrong. I got a dataframe thats size is (0,~14k). So I download the data, but I'm not sure how should I format it into a form that I can use.

I see 2 possibilities, I format the dataframe after download, which I will try to do. Or I download it in the correct format first, which would be the better and more elegant solution.

My problem that I don't know how to do the 2. and I may not succeed on the 1. thats why I make this post.

def get_stock_price_csv_from_poloniex():

    import requests
    from pandas import DataFrame
    from io import StringIO

    url = 'https://poloniex.com/public?command=returnChartData&currencyPair=USDT_ETH&start=1435699200&end=9999999999&period=14400'

    csv = requests.get(url)

    if csv.ok:
        return DataFrame.from_csv(StringIO(csv.text), sep=',')
    else:
        return None

Upvotes: 1

Views: 327

Answers (1)

Anonymous
Anonymous

Reputation: 12090

The source data is not CSV, it's . Luckily pandas provides facilities for working with it as well.

import requests
from pandas.io.json import json_normalize

url = 'https://poloniex.com/public?command=returnChartData&currencyPair=USDT_ETH&start=1435699200&end=9999999999&period=14400'
resp = requests.get(url)
data_frame = json_normalize(resp.json())

Upvotes: 3

Related Questions