python - 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 文件
解决方案
尝试使用该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)
推荐阅读
- visual-studio - Visual Studio:刷新“查找所有引用”结果
- r - 无法访问存储库的索引 https://macos.rbind.org/src/contrib
- javascript - 是否可以通过运行所有代码 try...catch 块来捕获所有 JS 异常?
- java - 使用 AWS S3 和 Lambda 处理大文件
- r - ggplot2 闪避条和图例未排序
- javascript - 发送邮件进度条
- r - 通过删除 R 中的重复项来计算多列
- android - Android ViewPager 具有相同类的所有片段而无需事务
- node.js - Sequelize 不生成连接表或获取/添加方法
- user-interface - Flutter:如何重现这个对手指运动有反应的底部容器