python - 如何将 Curl 的输出直接发送到 postgres 数据库
问题描述
我想直接将输出发送到 postgres 数据库!有可能我必须先将其转换为另一种格式!任何想法!!
curl -sG https://peeringdb.com/api/netixlan --data-urlencode asn=58717 \
--data-urlencode fields=ix_id,ipaddr4,ipaddr6,speed | jq -c '.data[]'
输出是:
{"ix_id":215,"speed":1000,"ipaddr4":"198.32.167.102","ipaddr6":null} {"ix_id":158,"speed":10000,"ipaddr4":"27.111.228.8 ","ipaddr6":"2001:de8:4::5:8717:1"} {"ix_id":158,"speed":10000,"ipaddr4":"27.111.229.92","ipaddr6":"2001 :de8:4::5:8717:2"} {"ix_id":429,"speed":10000,"ipaddr4":"103.16.102.35","ipaddr6":"2001:de8:12:100:: 35"}
解决方案
您可以尝试使用 Pandas 和 sqlalchemy:
import pandas as pd
from sqlalchemy import create_engine
#Define your connection to the DB:
engine = create_engine('postgresql://username:password@servername:port/dbname')
#Convert your json to a Pandas Dataframe:
df = pd.read_json(json_file) # Your json goes here,
#or just create a dataframe from the curl dictionary output
#Sends the DataFrame to postgres:
df.to_sql('tablename', engine, if_exists='replace', schema='public')
推荐阅读
- delphi - Global scope procedures cannot be generic? Is there a technical reason for this restriction?
- matlab - 连接不同大小的矩阵
- amazon-web-services - 如何将味精发送到设备并等待 aws IoT 的响应?
- java - 无法获得数组的平均值
- javascript - TypeError:this.setState 不是函数
- kotlin - 比较corda中的两种状态
- angular - Angular 2.0 通过 iFrame 与支付网关集成
- javascript - 如何找到具有 2 个类的元素
- simulation - 阅读 Ion Torrent 全外显子组测序模拟器
- python - StandarScaler,sklearn从我自己的计算中得到不同的结果?