作者:星罗 | 来源:互联网 | 2023-10-13 10:09
Python的list,dictionary可以写入file, 也可以从file中读取。
关于list:
1)写入文件
self.existedBlog.write("your item data" + "\n")
2)读取
self.existedBlog = open("existedBlog", "r+")
self.existedBlog.seek(0)
currentBlogs = self.existedBlog.readlines()
print(currentBlogs)
3)文件内容:
line1
line2
line3
line4
关于dictionay:
1)写入文件
import json
dict['authToken'] = "your dev auth token"
file = open('config', "w+")
writer = json.JSONEncoder()
print(writer.encode(dict).strip("{}"))
2)读取
import json
input_text = open('config').read()
input_json = "{%(input_text)s}" % vars()
reader = json.JSONDecoder()
config = reader.decode(input_json)
print(config)
3)文件内容:
"authToken":"your dev auth token",
"noteStoreUrl":"your store",
"blogType":"your blog api",
"username":"",
"password":"",