citybikes:JSON到数据帧

2024-06-11 19:16:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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文件


Tags: injsonpandasreadlineuserslistfile
1条回答
网友
1楼 · 发布于 2024-06-11 19:16:26

尝试使用^{}模块,如下所示:

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)

相关问题 更多 >