当前第2页 返回上一页
其中格式{key:[value1,value2,...]}
文本txt格式如下:
guaguashipinliaotianshi|.guagua.cn,
guaguashipinliaotianshi|iguagua.net,
guaguashipinliaotianshi|.17guagua.com,
jiuxiumeinvzhibo|.69xiu.com,
nbazhibo|.estream.cn,
youbo|yb.sxsapp.com,
其中第一列的名字有重复想要一个名字对应多个结果,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def makehostDict():
host_dict={}
f_allhost=open( 'xml_host.txt' , 'rb' )
lines=f_allhost.readlines()
for line in lines:
line_list=line.split( '|' )
name=line_list[0]
host=line_list[1].strip( '\n' )
if host is not '' :
if host_dict.has_key(name):
host_dict.get(name).append(host)#此处为关键向字典里已经有的key(name)值后继续添加value(host)
else :
host_dict.setdefault(name,[]).append(host)#创建{name,[host]}value为列表的格式的字典。
return host_dict
host_dict=makehostDict()
print host_dict
|
推荐教程:《python视频教程》
以上就是python字典中如何一键多值的写入?的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
qPython3l怎么用
Python和julia学哪个
Python怎么把列表转化为字符串
r语言和Python的详细对比
Python如何向集合追加元素
Python运行其他程序的方法实例详解
Python32位和64位有什么区别
Python中flask应用(表单处理)
Python中while循环打印星星的四种形状
Python会取代php吗?
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python字典中如何一键多值的写入?