<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### 1.說明 Json,全稱為JavaScript Object Notation,即JavaScript對象標記 json格式: ``` [{ "name": "Bob", "gender": "male", "birthday": "1992-10-18" }, { "name": "Selina", "gender": "female", "birthday": "1995-10-18" }] ``` ### 2.讀取Json 通過json庫實現對json文件的讀寫操作,調用json庫的loads\(\)方法將Json文本字符串轉為Json對象,可以通過dumps\(\)方法將Json對象轉為文本字符串 實例: ``` import json content = """ [{ "name": "Bob", "gender": "male", "birthday": "1992-10-18" }, { "name": "Selina", "gender": "female", "birthday": "1995-10-18" }] """ print(type(content)) content = json.loads(content) print(type(content)) print(content) ``` 運行結果: ``` <class 'str'> <class 'dict'> [{'name': 'Bob', 'gender': 'male', 'birthday': '1992-10-18'}, {'name': 'Selina', 'gender': 'female', 'birthday': '1995-10-18'}] ``` 注意: Json 的數據需要用雙引號來包圍,不能使用單引號,不然會拋出如下錯誤 ``` json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 5 (char 8) ``` 從json文本中讀取內容 ``` import json with open('test.json','r') as f: content = f.read() content = json.loads(content) print(content) ``` 運行結果: ``` [{'name': 'Bob', 'gender': 'male', 'birthday': '1992-10-18'}, {'name': 'Selina', 'gender': 'female', 'birthday': '1995-10-18'}] ``` ### 3.輸出json 利用dumps\(\)方法將json對象轉化為字符串,然后使用write\(\)方法寫入文本中 ``` import json content = [{ 'name': 'Bob', 'gender': 'male', 'birthday': '1992-10-18' }] with open('test2.json','w') as f: f.write(json.dumps(content)) ``` 如果json中包含中文字符,為了保證輸出正文,需要指定一個參數ensure\_ascii=False,另外還需要指定文件輸出的編碼 ``` import json content = [{ 'name': '天使', 'gender': '女', 'birthday': '1992-10-18' }] with open('test2.json','w',encoding='utf-8') as f: f.write(json.dumps(content,ensure_ascii=False,indent=2)) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看