更新時(shí)間:2020-10-09 來(lái)源:黑馬程序員 瀏覽量:
在Python中將數(shù)組轉(zhuǎn)為Json數(shù)據(jù)存儲(chǔ)時(shí)需要用到將json模塊中的json.dumps()或者json.dump()方法。
·json.dumps()方法用法
使用json.dumps( )方法將Python數(shù)組轉(zhuǎn)為json格式數(shù)據(jù)
# 導(dǎo)入json模塊 import json # 定義Python數(shù)組 py_list = [{'JavaEE': "http://java.itheima.com"}, {'HTML$JS+前端': "http://web.itheima.com"}, {'大數(shù)據(jù)': "http://cloud.itheima.com"}, {'Python': "http://python.itheima.com"}, {'人工智能': "http://ai.itheima.com"}, {'軟件測(cè)試': "http://test.itheima.com"}, {'C++': "http://c.itheima.com"}, {'UI/UE設(shè)計(jì)': "http://ui.itheima.com"}, {'產(chǎn)品經(jīng)理': "http://pm.itheima.com"}, {'Linux云計(jì)算+運(yùn)維開(kāi)發(fā)': "http://linux.itheima.com"}, {'新媒體+短視頻直播運(yùn)營(yíng)': "http://java.itheima.com"}, {'拍攝剪輯': "http://movies.itheima.com"}, {'智能機(jī)器人軟件開(kāi)發(fā)': "http://java.itheima.com"}, {'電商視覺(jué)運(yùn)營(yíng)設(shè)計(jì)': "http://java.itheima.com"},] # 使用json.dumps()方法轉(zhuǎn)為json格式數(shù)據(jù) # 注意:默認(rèn)會(huì)轉(zhuǎn)為二進(jìn)制數(shù)據(jù),使用 ensure_ascii=False 設(shè)置不轉(zhuǎn)為二進(jìn)制 json_data = json.dumps(py_list, ensure_ascii=False) print(json_data) # 打印json_data print(type(py_list)) # 打印py_list數(shù)據(jù)類型 print(type(json_data)) # 打印json_data數(shù)據(jù)類型
運(yùn)行結(jié)果為:
D:\logfenxi\venv\Scripts\python.exe D:/logfenxi/py_js.py[{"JavaEE": "http://java.itheima.com"}, {"HTML$JS+前端": "http://web.itheima.com"}, {"大數(shù)據(jù)": "http://cloud.itheima.com"}, {"Python": "http://python.itheima.com"}, {"人工智能": "http://ai.itheima.com"}, {"軟件測(cè)試": "http://test.itheima.com"}, {"C++": "http://c.itheima.com"}, {"UI/UE設(shè)計(jì)": "http://ui.itheima.com"}, {"產(chǎn)品經(jīng)理": "http://pm.itheima.com"}, {"Linux云計(jì)算+運(yùn)維開(kāi)發(fā)": "http://linux.itheima.com"}, {"新媒體+短視頻直播運(yùn)營(yíng)": "http://java.itheima.com"}, {"拍攝剪輯": "http://movies.itheima.com"}, {"智能機(jī)器人軟件開(kāi)發(fā)": "http://java.itheima.com"}, {"電商視覺(jué)運(yùn)營(yíng)設(shè)計(jì)": "http://java.itheima.com"}]
<class 'list'>
<class 'str'>
json.dump()方法用法
使用json.dump()方法將python 數(shù)組轉(zhuǎn)為json格式數(shù)據(jù)并存儲(chǔ)
# 導(dǎo)入json模塊 import json # 定義Python數(shù)組 py_list = [{'JavaEE': "http://java.itheima.com"}, {'HTML$JS+前端': "http://web.itheima.com"}, {'大數(shù)據(jù)': "http://cloud.itheima.com"}, {'Python': "http://python.itheima.com"}, {'人工智能': "http://ai.itheima.com"}, {'軟件測(cè)試': "http://test.itheima.com"}, {'C++': "http://c.itheima.com"}, {'UI/UE設(shè)計(jì)': "http://ui.itheima.com"}, {'產(chǎn)品經(jīng)理': "http://pm.itheima.com"}, {'Linux云計(jì)算+運(yùn)維開(kāi)發(fā)': "http://linux.itheima.com"}, {'新媒體+短視頻直播運(yùn)營(yíng)': "http://java.itheima.com"}, {'拍攝剪輯': "http://movies.itheima.com"}, {'智能機(jī)器人軟件開(kāi)發(fā)': "http://java.itheima.com"}, {'電商視覺(jué)運(yùn)營(yíng)設(shè)計(jì)': "http://java.itheima.com"},] with open('js_str.json', 'w') as json_data: json.dump(py_str, json_data, ensure_ascii=False)
運(yùn)行結(jié)果:
D:\logfenxi\venv\Scripts\python.exe D:/logfenxi/py_js.py
Process finished with exit code 0
生成js_str.josn文件
存儲(chǔ)的json格式數(shù)據(jù)
猜你喜歡: