python指令碼解析json檔案

2022-04-07 09:02:54 字數 2319 閱讀 1356

python指令碼解析json檔案

沒寫完。但是有效果。初次嘗試,寫的比較不簡潔。。。 

比較煩的地方在於:

1,中文編碼:

pspecs.decode('raw_unicode_escape')

2,花括號轉義:

\n### [format]\n```json\n ":\"\"\n }}\n```\n### [parameters]\n* ;;屬性說明.\n* specs:\n\n\n

'def

writeservices(jsonobj):

print

(jsonobj)

pname = jsonobj["

name"];

#ptype = jsonobj["type"];

defwriteproperty( jsonobj ):

pname = jsonobj["

name"];

property = jsonobj["

identifier"];

ptype = jsonobj["

datatype

"]["

type"]

pspecs = json.dumps(jsonobj["

datatype

"]["

specs"])

print(pspecs.decode('

raw_unicode_escape

')) //解決中文編碼問題

#print(protocol_templete.format(pname="".join(pname),ptype=ptype,pspecs=pspecs,property="".join(property)))

#writefile(protocol_templete)

writefile(protocol_templete.format(pname="".join(pname),ptype=ptype,pspecs=pspecs.decode('

raw_unicode_escape

'),property=""

.join(property)))

defwriteevent(jsonobj):

print

(jsonobj)

#追加檔案內容

defwritefile(str):

with open(md_file, 'a+

') as fo:

fo.write(str)

fo.close();

defhandlejson(alinkdic):

#print(str(alinkdic)) # services = alinkdic["services"]# print(services)

for k in

alinkdic.keys():

list = ["

services

","events

","properties"]

if(k in

list) :

writefile(

"## %s\n

"%k)

values = alinkdic[k] #

list

if(k == "

services"):

map(writeservices,values)

elif(k == "

events"):

map(writeevent,values)

else

: map(writeproperty,values)

if__name__ == '

__main__':

ifos.path.exists(json_file):

filecontent =open(json_file).read();

#清空檔案

with open(md_file, '

wb+'

) as file:

file.close();

jsondic = json.loads(filecontent) #

print(open(json_file).read()); //列印json檔案

handlejson(jsondic) #

print(json.loads(''.join(open(json_file).readlines()))) //json物件轉換成python物件

else

:

print

'json 配置檔案不存在

'

python 解析多層json

原始檔案內容 channels 3 height 1080 width 1920 修改並儲存 coding utf 8 import os import json 獲取目標資料夾的路徑 filedir r j numberdata mrcnnhik test 獲取資料夾中的檔名稱列表 filenam...

Python解析JSON詳解

json 函式 使用 json 函式需要匯入 json 庫 import json。函式 描述 json.dumps 將 python 物件編碼成 json 字串 json.loads 將已編碼的 json 字串解碼為 python 物件 json.dumps 語法 json.dumps obj,s...

python遞迴解析JSON

知識點 1 編碼設定 特殊字元處理 2 正規表示式提取json字串 3 遞迴列印json屬性值 usr bin python coding utf 8 import os,sys,time import urllib.request,requests,bs4 import re,json,demjs...