python3中json.dump()與json.dumps()的區別


1.json.dump()是通過文件的形式進行處理,舉個栗子:

 1  import json  2 
 3  my_dict = dict()  4  with open('labels.txt', 'r', encoding='utf-8') as f:  5      contents = f.readlines()  6      for content in contents:  7         key = content.split(':')[0]  8         label_1 = content.split(':')[1].split(',')[0]  9         label_2 = content.split(':')[1].split(',')[1] 10         value = [label_1, label_2] 11         my_dict[key] = value 12 
13 with open('./filename.json', 'w', encoding='utf-8') as f: 14     json.dump(my_dict, f)

label.txt內容如下:

2:'地地意同了H', 'rreree'
4:'3YP65G', 'ybbreb'

2.json.dumps()是將字典轉換為json,不通過文件,舉個栗子

import json data = { 'name' : 'soap', 'shares' : 90, 'price' : 542.23 } json_str = json.dumps(data)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM