本文目录一览:
python中以字典为元素的列表怎么写入文本文件
##调用json.dump函数就行了,下面是一个例子
import json
##将FileName指定为你的文件的实际路径
FileName='t.txt'
lst=[{'name':'lili','age':25},{'name':'gg','age':18}]
with open(FileName,'w') as f:
json.dump(lst,f)
python字典存入txt 急!!!
# -*- coding: cp936 -*-
import os
write_file = os.getcwd() + '\dit.txt'
print write_file
output = open(write_file,'w')
dic = {2:'bob',3:'alice'}
for i in dic:
print i,dic[i]
write_str = str(i) + ' ' + dic[i] + '\n'
output.write(write_str)
output.close()
试试看咯。
python中中的字典怎么写入文本文件?
##注意:最左边每个=表示一个空格
f='你的文件名.txt'
d={('dsaa','dsa'):1.2132,('fdsfsd','dsada'):2.3243}
with open(f,'w') as fo:
====for k,v in d.items():
========print("%s,%s %.4f" %(k[0],k[1],v),sep='',file=fo)