python文件讀取 ,json文件的存儲


with open('C:\Users\10224298\Desktop\桌面文件') as file_object:#with在不需要訪問文件后將其關閉。

     contents = file_object.read()
    print(contents.rstrip())         rstrip可以刪除每行末尾的換行符 

   使用jason模塊保存信息:

     json可以在python之間分享數據,並可以與其他程序語言一起分享。

   json.dump()接受兩個參數,要存儲的數據和用於存儲數據的文件對象。

   import json

  numbers = [2,3,4,5]

 filename="numbers.json"

 with open(filename,"w") as f_obj:

           json.dump(numbers,f_obj)

    json.load()    是指將列表導入內存中

    import json

   filename="numers.json"

  with open(filename) as f_obl:

      numbers = json.load(f_obj)

print (numbers)

 


免責聲明!

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



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