目录
一、文件读写
filename = "./output/data.txt"
# 文件写入
with open(filename, 'w', encoding='utf-8') as myfile:
myfile.write("你好,Python!\n")
myfile.write("hello Python!\n")
# 文件读出
with open(filename, 'r', encoding='utf-8') as myfile:
content = myfile.read()
print(content)
二、json对象读写
import json
filename = "output/data.json"
mydata = {
"user": {
"username": "lijiang",
"password":123,
},
"age": 22
}
# json对象写入
with open(filename, 'w', encoding='utf-8') as myfile:
json.dump(mydata, myfile, indent=4)
# json对象读出
with open(filename, 'r', encoding='utf-8') as myfile:
myjson = json.load(myfile)
print(myjson)