python27 读取嵌套的字典

python27 读取嵌套的字典,第1张

python27 读取嵌套的字典 python27 读取嵌套的字典
items = {"NAME":{"label":"名称","fieldName":"NAME"},"ADDRESS":{"label":"地址","fieldName":"ADDRESS"},"LON":{"label":"经度","fieldName":"LON"},"LAT":{"label":"纬度","fieldName":"LAT"},"SOURCE":{"label":"数据来源","fieldName":"SOURCE"},"CITYCODE":{"label":"城市代码","fieldName":"CITYCODE"},"PRODATE":{"label":"处理日期","fieldName":"PRODATE"}}

'''
#遍历列表,读要素
for x in items:
    print "fieldName1 = %s" % x
'''

#用enumerate函数遍历list
for i,j in enumerate(items):
     print "fieldName%d = %s" % (i,j)

最终得到这样的结果

fieldName0 = CITYCODE
fieldName1 = NAME   
fieldName2 = LON    
fieldName3 = PRODATE
fieldName4 = SOURCE
fieldName5 = ADDRESS
fieldName6 = LAT

有时,因为输出需要,字段必须保留""。所以,在双引号前面加个转义符 ,即反斜杠。如"Hello "W “orld”,会输出 Hello "W"orld。

例如上面的语句

# -*- coding: utf-8 -*-

items = {"NAME":{"label":"名称","fieldName":"NAME"},"ADDRESS":{"label":"地址","fieldName":"ADDRESS"},"LON":{"label":"经度","fieldName":"LON"},"LAT":{"label":"纬度","fieldName":"LAT"},"SOURCE":{"label":"数据来源","fieldName":"SOURCE"},"CITYCODE":{"label":"城市代码","fieldName":"CITYCODE"},"PRODATE":{"label":"处理日期","fieldName":"PRODATE"}}

'''
#遍历列表,读要素
for x in items:
    print "fieldName1 = %s" % x
'''

#用enumerate函数遍历list
for i,j in enumerate(items):
     print "fieldName%d = "%s"" % (i,j)

输出结果为

fieldName0 = "CITYCODE"
fieldName1 = "NAME"
fieldName2 = "LON"
fieldName3 = "PRODATE"
fieldName4 = "SOURCE"
fieldName5 = "ADDRESS"
fieldName6 = "LAT"

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5680286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存