亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python實現(xiàn)對json文件的讀寫

系統(tǒng) 1861 0

目錄

1. 從json文件讀取數(shù)據(jù)

2. 將數(shù)據(jù)寫入json文件

方法一:使用dump()函數(shù)

方法二:使用dumps()函數(shù)

完整代碼

流程

json文件

Python 腳本

運行結(jié)果

控制臺

base1.json

base2.json


?

1. 從json文件讀取數(shù)據(jù)

?

使用load()函數(shù)獲取json文件中的數(shù)據(jù),并轉(zhuǎn)換為Python的內(nèi)置數(shù)據(jù)類型(列表或字典)。

下面自定義的函數(shù)read_json_file()實現(xiàn)了讀取json文件數(shù)據(jù)的功能:

            
              def read_json_file(url):
    """
    Get the data for the json file.
    :param url: 
              
                 json file path.
    :return: 
                
                   json file data.
    """
    with open(url, "r") as json_file:
        data = json.load(json_file)
    json_file.close()

    return data
                
              
            
          

?

2. 將數(shù)據(jù)寫入json文件

?

方法一:使用dump()函數(shù)

dump()函數(shù)可以將Python中的列表或字典寫入到j(luò)son文件中。可選參數(shù)indent指定縮進。

下面自定義的函數(shù)write_json_file_a()將數(shù)據(jù)寫入到j(luò)son文件中。

            
              def write_json_file_a(data):
    """
    Write Json file.
    :param data: 
              
                 data
    :return: 
                
                  
    """
    path = "base1.json"

    with open(path, "w") as json_file:
        json.dump(data, json_file, indent=6)
    json_file.close()
    print("Write %s success." % path)
    return
                
              
            
          

?

方法二:使用dumps()函數(shù)

dumps()函數(shù)跟dump()函數(shù)的用法有所不同。dumps()先將Python列表或字典轉(zhuǎn)換成特定的字符串格式,再借用open()和write()函數(shù)將轉(zhuǎn)換后的數(shù)據(jù)寫入到j(luò)son文件中。可選參數(shù)indent指定縮進。

下面自定義的函數(shù)write_json_file_b()將數(shù)據(jù)寫入到j(luò)son文件中。

            
              def write_json_file_b(data):
    path = "base2.json"

    data = json.dumps(data, indent=4)
    open(path, "w").write(data)
    print("Write %s success." % path)
    return
            
          

?

?

完整代碼

?

流程

代碼從base.json讀取數(shù)據(jù),再將讀取后的數(shù)據(jù)分別寫入base1.json文件(6縮進)和base2.json文件(4縮進)。

?

json文件

待讀取的base.json如下所示:

            
              [
  {
    "name": "iphone X",
    "manufacturer": "Apple",
    "type": "mobile phone"
  },
  {
    "name": "HuaweiCloud",
    "manufacturer": "Huawei",
    "type": "Virtual cloud technology"
  },
  {
    "name": "Android",
    "manufacturer": "Google",
    "type": "mobile machines system"
  }
]
            
          

?

Python 腳本

Python腳本在獲取到j(luò)son數(shù)據(jù)后打印在控制臺上。然后分別使用兩種寫json文件的方式寫入數(shù)據(jù)。

            
              """
Note:
    Json actions demo.
    1. Get Json data.
    2. Write Json file.
"""

import json


def read_json_file(url):
    """
    Get the data for the json file.
    :param url: 
              
                 json file path.
    :return: 
                
                   json file data.
    """
    with open(url, "r") as json_file:
        data = json.load(json_file)
    json_file.close()

    return data


def write_json_file_a(data):
    """
    Write Json file.
    :param data: 
                  
                     data
    :return: 
                    
                      
    """
    path = "base1.json"

    with open(path, "w") as json_file:
        json.dump(data, json_file, indent=6)
    json_file.close()
    print("Write %s success." % path)
    return


def write_json_file_b(data):
    path = "base2.json"

    data = json.dumps(data, indent=4)
    open(path, "w").write(data)
    print("Write %s success." % path)
    return



if __name__ == "__main__":
    data = read_json_file("base.json")
    print("data: \n%s" % data)

    write_json_file_a(data)
    write_json_file_b(data)
                    
                  
                
              
            
          

?

?

運行結(jié)果

?

控制臺

            
              data: 
[{'name': 'iphone X', 'manufacturer': 'Apple', 'type': 'mobile phone'}, {'name': 'HuaweiCloud', 'manufacturer': 'Huawei', 'type': 'Virtual cloud technology'}, {'name': 'Android', 'manufacturer': 'Google', 'type': 'mobile machines system'}]
Write base1.json success.
Write base2.json success.

Process finished with exit code 0

            
          

?

base1.json

base1文件中采用了6縮進寫入。

            
              [
      {
            "name": "iphone X",
            "manufacturer": "Apple",
            "type": "mobile phone"
      },
      {
            "name": "HuaweiCloud",
            "manufacturer": "Huawei",
            "type": "Virtual cloud technology"
      },
      {
            "name": "Android",
            "manufacturer": "Google",
            "type": "mobile machines system"
      }
]
            
          

?

base2.json

base2文件中采用了4縮進寫入。

            
              [
    {
        "name": "iphone X",
        "manufacturer": "Apple",
        "type": "mobile phone"
    },
    {
        "name": "HuaweiCloud",
        "manufacturer": "Huawei",
        "type": "Virtual cloud technology"
    },
    {
        "name": "Android",
        "manufacturer": "Google",
        "type": "mobile machines system"
    }
]
            
          

?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 干一干操一操 | 四虎永久地址入口 | 久久亚洲福利 | 奇米影视奇米色777欧美 | 青草免费免费观看视频在线 | 欧美日韩在线播放一区二区三区 | 91色吧| 欧美精品国产综合久久 | 特大一级aaaaa毛片 | 亚洲人成网站色7799在线观看 | 亚洲精品国产第一区二区尤物 | 国产麻豆永久视频 | 国产99福利视频在线 | 欧美久久网 | 九九精品久久久久久噜噜 | 香蕉视频黄网站 | a欧美在线| 欧美在线精品一区二区三区 | 久久精品国产清白在天天线 | 亚洲国产成人九九综合 | 777奇米影视久久激情日韩欧美 | 亚洲精品成人 | 欧美日本一本 | 中文字幕日韩欧美 | 日本一区二区网站 | 奇米影视在线 | 福利一区视频 | 免费精品国产 | 天天干天天拍天天射 | 99r视频| 欧美成人性videos | 日韩欧美精品综合一区二区三区 | 久久99综合国产精品亚洲首页 | 热99精品在线 | 国产不卡视频在线播放 | 亚洲性生活 | 五月天婷婷在线观看 | 色综合久久98天天综合 | 26uuu欧美日韩国产 | 欧洲老妇bbbbbxxxxx | 波多野结衣在线一区二区 |