请问如何用python将字典转换到txt文本中?
字典的类型如下dic={'11542':{'68784':5.0,'43485':5.0,'83646':5.0,'109754':5.0,'119735':3.0,'4...
字典的类型如下
dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
希望出来的结果是
11542 68784 5.0
11542 43485 5.0
……
请高手赐教 展开
dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
希望出来的结果是
11542 68784 5.0
11542 43485 5.0
……
请高手赐教 展开
5个回答
展开全部
from __future__ import print_function
dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
dicfile=open('dic.txt','w')
for key in dic:
for innerkey in dic[key]:
print('{0:<10}{1:<10}{2}'.format(key,innerkey,dic[key][innerkey]),file=dicfile)
dicfile.close()
字典是无序的,因此输出的顺序不确定,如果要有序输出只能按大小排序后输出
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
>>> dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
>>> {print(d, k, v) for d in dic for (k, v) in sorted(dic[d].items())}
11542 109754 5.0
11542 119735 3.0
11542 119736 5.0
11542 42640 2.0
11542 43485 5.0
11542 68784 5.0
11542 69983 5.0
11542 83646 5.0
{None}
写入文件的话,可以这样:
file = open(path, 'w') #dir是对应的路径
dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
{file.write(d, k, v) for d in dic for (k, v) in sorted(dic[d].items())}
>>> {print(d, k, v) for d in dic for (k, v) in sorted(dic[d].items())}
11542 109754 5.0
11542 119735 3.0
11542 119736 5.0
11542 42640 2.0
11542 43485 5.0
11542 68784 5.0
11542 69983 5.0
11542 83646 5.0
{None}
写入文件的话,可以这样:
file = open(path, 'w') #dir是对应的路径
dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
{file.write(d, k, v) for d in dic for (k, v) in sorted(dic[d].items())}
追问
我的是2.73版本的python,在eclipse中进行调试。写入文件调试出现问题,出错如下TypeError: function takes exactly 1 argument (3 given)。
追答
抱歉才看到,因为没用文件测试我忘了write一次只能写一个- -
file.write(d, k, v) -> file.write(' '.join([d, k, v]))就行了,不过反正问题解决了...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
# coding=utf-8
def Trans(dic):
rt=[]
try:
fp = open("dict.txt",'w')
for (k,v) in dic.items():
for (ik,iv) in v.items():
fp.write('%-10s,%-10s,%-10s\n' %(k,ik,iv))
return rt
finally:
fp.close()
if __name__=="__main__":
dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
rt=Trans(dic)
def Trans(dic):
rt=[]
try:
fp = open("dict.txt",'w')
for (k,v) in dic.items():
for (ik,iv) in v.items():
fp.write('%-10s,%-10s,%-10s\n' %(k,ik,iv))
return rt
finally:
fp.close()
if __name__=="__main__":
dic={'11542': {'68784': 5.0, '43485': 5.0, '83646': 5.0, '109754': 5.0, '119735': 3.0, '42640': 2.0, '69983': 5.0, '119736': 5.0}}
rt=Trans(dic)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上的是正解啊?莫非你还有什么特殊的需求?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询