您的位置:首页 > 编程语言 > Python开发

Python 中list, dictionary 与 file相互操作

2013-07-10 15:45 295 查看



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":"",
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: