欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python - json.dumps - json.loads - requests.get

程序员文章站 2024-02-03 11:12:34
...
import numpy as np
import pandas as pd
from pandas import Series, DataFrame   # pandas 数据分析包

#JSON数据
obj = """ 
{"name": "Wes",
 "places_lived": ["United States", "Spain", "Germany"],
 "pet": null,
 "siblings": [{"name": "Scott", "age": 25, "pet": "Zuko"},
              {"name": "Katie", "age": 33, "pet": "Cisco"}]
}
"""

import json
result = json.loads(obj)  #解码python json格式,可以用这个模块的json.loads()函数的解析方法
#print( result )

asjson = json.dumps(result) #json.dumps是将一个Python数据类型列表进行json格式的编码解析
#print( asjson )

_list = ['iplaypython',[1,2,3], {'name':'xiaoming'}]
_list_to_json = json.dumps(_list) # 将一个list列表对象,进行了json格式的编码转换
#print( _list_to_json )
'''
python 3.x 之前

json.dumps:dict转成str        json.dump是将python数据保存成json
json.loads:str转成dict        json.load是读取json数据

json.dump和json.dumps很不同,json.dump主要用来json文件读写,和json.load函数配合使用。

python 3.x 之后只剩下 dumps 和 loads
'''

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

#二进制数据格式
#pickle
frame = pd.read_csv('data/ex1.csv')
frame
frame.to_pickle('data/frame_pickle')

pd.read_pickle('data/frame_pickle')

#HDF5格式
store = pd.HDFStore('mydata.h5')
store['obj1'] = frame
store['obj1_col'] = frame['a']
store

store['obj1']

store.close()
#os.remove('mydata.h5')

#使用HTML和Web API
import requests
url = 'https://api.github.com/repos/pydata/pandas/milestones/28/labels'
resp = requests.get(url)
resp

data=json.loads(resp.text)

issue_labels = DataFrame(data)
#print( issue_labels )