其他分享
首页 > 其他分享> > JSON 用法

JSON 用法

作者:互联网

1.json.loads()加载json文件为python对象

obj='''{"name":"wes",
"places_lived":["united states","german","spain"],
"pet":"a",
"siblings":[{"name":"scott","age":30,"pets":["zeus","zuko"]},
{"name":"katie","age":20,"pets":["se","me","dou"]}]
}'''

result=json.loads(obj)
print(result)

2.json.dumps()将python对象转会json

asjson=json.dumps(result)

3.将1中的python列表转化为dataframe文件

siblings=pd.DataFrame(result['siblings'],columns=['name','age'])
print(siblings)

4.python.read_json可以自动将json数据集转化为series或者dataframe

5.to_json将数据集pandas输出至json

a=pd.read_csv('/Users/youshiqi/lianghaiming/Desktop/shiyan.csv')
print(a.to_json(orient='records'))
In [71]: print(data.to_json())
 {"a":{"0":1,"1":4,"2":7},"b":{"0":2,"1":5,"2":8},"c":{"0":3,"
 

In [72]: print(data.to_json(orient='records'))
 [{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]

 

标签:name,python,json,用法,JSON,result,print,siblings
来源: https://www.cnblogs.com/lianghaiming/p/13943838.html