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)
