Python 中list, dictionary 与 file相互操作
2013-07-05 09:30
381 查看
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 jsonwriter = json.JSONEncoder()
print(writer.encode(dict).strip("{}"))
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":"",
相关文章推荐
- Python 中list, dictionary 与 file相互操作
- Python 中list, dictionary 与 file相互操作
- Python的list、tuple、set、dictionary操作总结
- python中的list、dict、string基本操作
- 第二百九十七节,python操作redis缓存-List类型,可以理解为列表
- Python——array与list相互转换
- python list string相互转换
- python list 与 array 相互转换
- Python操作列表之List.insert()方法的使用
- Python操作列表之List.insert()方法的使用
- python入门教程、基本类型的操作及相互转换
- Python进阶(十五)-file文件操作
- Python中列表list以及list与数组array的相互转换实现方法
- Python使用os模块和fileinput模块来操作文件目录
- python中的list如何进行相减操作或者将list分片
- Python对list的操作描述
- Python中list的详细操作描述(举例说明)
- python list file in certain diretory
- python list和tuple相互转化
- Python list 常用操作