ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 软件工程 >> python 列表(list)元组(tuple)字典(dict)如何打印中文总结

python 列表(list)元组(tuple)字典(dict)如何打印中文总结(2/2)

来源:网络整理     时间:2016-08-12     关键词:

本篇文章主要介绍了" python 列表(list)元组(tuple)字典(dict)如何打印中文总结",主要涉及到方面的内容,对于软件工程感兴趣的同学可以参考一下: python中,如果直接使用print去打印含中文元素的list、tuple、dict,并不能打印出中文字符,而是打印出unicode编码,例如:tuple1 ...

dict6 = {1:'淘宝', 2:'京东', 3:'天猫', 4:'1号店', 5:'美团'}

#方法一:使用格式化打印字典数据
dstring1 = ''
for eachKeys in dict6.keys():
    fill = '(%s : %s)' % (eachKeys, dict6[eachKeys])
    dstring1 += fill
    if dict6.keys().index(eachKeys) + 1 != len(dict6):
        dstring1 += ','

print dstring1

#方法二:非格式化打印字典数据,数据没有保存到字符串中
i = 0
string1 = ''
for eachKeys in dict6.keys():
    if i < (len(dict6)-1):
        print ('(' + str(eachKeys) + ' : ' + dict6[eachKeys] + '),'),
        i += 1
    else:
        print ('(' + str(eachKeys) + ': ' + dict6[eachKeys] + ')')

#方法三:方法二微调,把数据保存到字符串中
i = 0
string1 = ''
for eachKeys in dict6.keys():
    if i < (len(dict6)-1):
        fill = '(' + str(eachKeys) + ' : ' + dict6[eachKeys] + '),'
        string1 += fill
        i += 1
    else:
        fill = '(' + str(eachKeys) + ' : ' + dict6[eachKeys] + ')'
        string1 += fill
print string1

#方法四:直接去掉转义字符\,然后打印出来
lstring = str(dict6).decode('string_escape')
print lstring
输入结果:
(1 : 淘宝),(2 : 京东),(3 : 天猫),(4 : 1号店),(5 : 美团)
(1 : 淘宝), (2 : 京东), (3 : 天猫), (4 : 1号店), (5: 美团)
(1 : 淘宝),(2 : 京东),(3 : 天猫),(4 : 1号店),(5 : 美团)
{1: '淘宝', 2: '京东', 3: '天猫', 4: '1号店', 5: '美团'}

最后一个方法是最简单,而且可以原样打印出来~~~

以上就介绍了 python 列表(list)元组(tuple)字典(dict)如何打印中文总结,包括了方面的内容,希望对软件工程有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_3228042_2.html

相关图片

相关文章