首页 > 解决方案 > citybikes:JSON 到数据框

问题描述

我正在使用 python-citybikes ( https://pypi.org/project/python-citybikes/ ) 来检索一些数据。但是,我想不出一种导出数据的方法

import citybikes
import pandas as pd
client = citybikes.Client()
GlasgowNextBike = citybikes.Network(client, uid='nextbike-glasgow')
list(GlasgowNextBike.stations)
Stations = list(GlasgowNextBike.stations)
 pd.read_json(Stations)

我正进入(状态

Traceback (most recent call last):

  File "<ipython-input-15-5a1904def0e8>", line 1, in <module>
    pd.read_json(Stations)

  File "/Users/noor/opt/anaconda3/lib/python3.7/site-packages/pandas/util/_decorators.py", line 214, in wrapper
    return func(*args, **kwargs)

  File "/Users/noor/opt/anaconda3/lib/python3.7/site-packages/pandas/io/json/_json.py", line 585, in read_json
    path_or_buf, encoding=encoding, compression=compression

  File "/Users/noor/opt/anaconda3/lib/python3.7/site-packages/pandas/io/common.py", line 200, in get_filepath_or_buffer
    raise ValueError(msg)

ValueError: Invalid file path or buffer object type: <class 'list'>

我的问题是:

如何将结果导出/保存为 JSON 或 CSV 文件

标签: pythonjsoncsv

解决方案


尝试使用该json模块,如下所示:

import citybikes, json

client = citybikes.Client()
GlasgowNextBike = citybikes.Network(client, uid='nextbike-glasgow')

with open('GlasgowNextBike.json', 'w') as f:
    json.dump(GlasgowNextBike.data, f, indent=2)

推荐阅读