【问题标题】:python dictionary formatted printoutpython字典格式的打印输出
【发布时间】:2014-11-04 20:39:15
【问题描述】:

我正在尝试以格式化的方式为每个键打印一个包含两个值的字典,其中键与值分开,并且没有任何括号或逗号,只是平面文本缩进,例如:

--- 我的书签管理器 ---

号码__________姓名_________________地址

1__________Google 学术搜索______________网站1

代替:

--- 我的书签管理器 ---

号码_______________姓名_________________地址

_ _

1(“谷歌学术”、“网站 1”)

这是我的代码:

web_pages = {1:('Google Scholar','website1'),
         2:('Moodle','website2'),
         3:('BBC','website3'),
         4:('Webmail','website4')}

##-------output_structure_top---------------------------
def output_structure_top ():
    print "--- MY BOOKMARK MANAGER --- "+"\n"  
    print "Number"+" "*10 +"Name"+" "*30+"Address \n"
    print "-"*60+"\n"

##----output_structure_bottom------------------------
def output_structure_bottom():
    print "-"*60+"\n"

##----------------------------------------------------

output_structure_top ()
for key, value in web_pages.iteritems():
    print key, value
output_structure_bottom()

【问题讨论】:

    标签: python dictionary format key


    【解决方案1】:
    web_pages = {1:('Google Scholar','website1'),
             2:('Moodle','website2'),
             3:('BBC','website3'),
             4:('Webmail','website4')}
    
    ##-------output_structure_top---------------------------
    def output_structure_top ():
        print "--- MY BOOKMARK MANAGER --- "+"\n"  
        print "Number"+" "*10 +"Name"+" "*30+"Address \n"
        print "-"*60+"\n"
    
    ##----output_structure_bottom------------------------
    def output_structure_bottom():
        print "-"*60+"\n"
    
    ##----------------------------------------------------
    def output_structure_value(key,value):
        print (str(key) +" "*10+ value[0] +" "*30 +value[1])
    
    
    output_structure_top ()
    for key, value in web_pages.iteritems():
        output_structure_value(key,value)
    output_structure_bottom()
    

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多