Python 中list, dictionary 与 file相互操作

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":"",
posted @ 2013-07-05 09:30  muzizongheng  阅读(329)  评论(0编辑  收藏  举报
如果我们时时忙着展现自己的知识, 将何从忆起成长所需的无知?