简体   繁体   English

从URL下载Python 2.7 csv

[英]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. 我创建了一个从URL下载数据的功能,但格式错误。 I got a dataframe thats size is (0,~14k). 我得到了一个大小为(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. 我看到了2种可能性,我在下载后格式化数据帧,我将尝试这样做。 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. 我的问题是我不知道如何做2.我可能不会成功1.这就是为什么我发这个帖子。

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

The source data is not CSV, it's . 源数据不是CSV,它是 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM