Credit python read the disc source sells build json files processing

Three pandas json papers read
credit sale source disk structures Q <115.28.8.00.9.9>
from pandas.io.json Import json_normalize
Import pandas AS PD
Import json
Import Time

# 读入数据
data_str = open('AgriculturalDisease_train_annotations.json').read()

#———————————————————— 测试json_normalize ————————————————————
start_time = time.time()
for i in range(0, 300):
data_list = json.loads(data_str)
df = json_normalize(data_list)
end_time = time.time()
print (end_time - start_time)#耗时109秒

#———————————————————— 测试自己构造 ————————————————————
start_time = time.time()
for i in range(0, 300):
data_list = json.loads(data_str)
data = [[d["disease_class"], d["image_id"]] for d in data_list]
df = pd.DataFrame(data, columns=["disease_class", "image_id"])
end_time = time.time()
print (end_time - start_time)#耗时22秒

#———————————————————— 测试read_json ————————————————————
start_time = time.time()
for i in range(0, 300):
df = pd.read_json(data_str, orient='records')
end_time = time.time()
print (end_time - start_time)#耗时36秒

# read_json
df2 = pd.read_json(data_str, orient='records')

# 自己构造
data_list = json.loads(data_str)
data = [[d["disease_class"], d["image_id"]] for d in data_list]
df = pd.DataFrame(data, columns=["disease_class", "image_id"])
df.head(5)

Are the following three kinds of codes output
disease_class the image_id
0 62fd8bf4d53a1b94fbac16738406f10b.jpg. 1
. 1. 1 0bdec5cccbcade6b6e94087cb5509d98.jpg
2 8951e940341f77c8d361c1872c67b16d.jpg. 1
. 3. 1 7ed158da58c451f75fb790530d6f19cc.jpg
. 4. 1 9b7399aa-1c3c-4137-ae4e-196cd23fe573 ___ FREC_Sc ...
techniques: string collation complex json re-read into the following format, and then use data_list = json.loads (data_str) to read
{ "error_code": 40007, " error_msg": "fail to recognize"}

[{ "Department": "abcdef",
"query_result": { "code": "1000", "Description": "1000"},
"is_invoice":. 1,
"imageName": "./imgs/8888888.jpeg ",
" reco_result ": {" Total ":" "," invoice_no ":" 123 "," CREATE_DATE ":" "," check_code ":" "}}]
bulk reading json file (Chinese json)
./out_file json the two files as follows:
out_01.txt content: "{" name_ID ":" 12343 "," name ":" San "," identification code ":" unknown "}"
out_02.txt content: " { "name_ID": "12344" , "name": " John Doe", "identification code": "98983"} "

Guess you like

Origin www.cnblogs.com/yanxiayan/p/11275195.html