【问题标题】:Python: List of dictionaries: How to - nice printout using sys.stdoutPython:字典列表:如何使用 sys.stdout 进行漂亮的打印输出
【发布时间】:2017-12-07 09:49:40
【问题描述】:

我有以下词典列表

[{'Sequence': 'TGACCCTGCTTGGCGATCCCGGCGTTTC', 'Start': '52037', 'Strand': '+', 'End': '52064'}, {'Sequence': 'TGATCGCGCAACTGCAGCGGGAGTTAC', 'Start': '188334', 'Strand': '+', 'End': '188360'}, {'Sequence': 'TGATCCGGCACTCGTTGGAGTTCGTATC', 'Start': '245882', 'Strand': '+', 'End': '245909'}, {'Sequence': 'TGACCTTGGTCAGATCGATGACCGTAAT', 'Start': '318422', 'Strand': '+', 'End': '318449'}, {'Sequence': 'TGACCCGCGTATTTTGGAGCAGAAGTATC', 'Start': '343421', 'Strand': '+', 'End': '343449'}, {'Sequence': 'TGATCCGGCACTCGTTGGAGTTCGTATC', 'Start': '359576', 'Strand': '+', 'End': '359603'}, {'Sequence': 'TGATCGGCAATTCCTATGGCAAGTATC', 'Start': '457457', 'Strand': '+', 'End': '457483'}, {'Sequence': 'TGATCGAGGCGCCAGTTGTGCCCGTATT', 'Start': '627296', 'Strand': '+', 'End': '627323'}, {'Sequence': 'TGATCCAAGTGAACCCCCGCCCAGTAAA', 'Start': '637265', 'Strand': '+', 'End': '637292'}, {'Sequence': 'TGACCGGAAGACCGCCGTCGAGCGTATC', 'Start': '829035', 'Strand': '+', 'End': '829062'}, {'Sequence': 'TGATCGCGGCACCGACACCGGTCGTAAT', 'Start': '864440', 'Strand': '+', 'End': '864467'}, {'Sequence': 'TGATCGTTTCCGTGCTCGGAGCGTATC', 'Start': '934160', 'Strand': '+', 'End': '934186'}, {'Sequence': 'TGACCTGGCTCGAGGCCTGAGGAGTAAA', 'Start': '1162006', 'Strand': '+', 'End': '1162033'}, {'Sequence': 'TGACCCGCCGCAACATCCCCTTCGTAAA', 'Start': '1294515', 'Strand': '+', 'End': '1294542'}]

我想获得以下输出(通过 sys.stdout 重定向打印):

Start:1234  End:5678  Strand:+  Sequence:ABCDEFG
...

到目前为止,我的想法涉及 循环 遍历列表并使用 get-method 获取键的值,然后 手动打印输出,基本上是这样的:

for item in dictList:
    print("Start"+item.get('Start'))
    print("End"+item.get('End'))
    ...

我不确定如何处理换行符。我阅读了有关 sys.stdout 刷新以避免 换行符,但由于我不想避免所有换行符,我不确定这是否适合我的目标。

有没有更聪明/更短的方法来做到这一点?

更新

感谢 Aleksey,我想将我的打印声明更改为:

for item in dictList:
    print("Start"+str(item.get('Start'))+"  "+"End"+str(item.get('End'))+"  "+"Strand"+str(item.get('Strand'))+"  "+"Sequence: "+str(item.get('Sequence')))

更新2

到目前为止,我坚持这一点:

print(ecf+"  "+replicon+"  -  "+"Validated results: Total number of hits: "+str(len(finalList)))
print("")

for item in finalList:
    print("Start: "+str(item.get('Start'))+"  "+"End: "+str(item.get('End'))+"  "+"Strand: "+str(item.get('Strand'))+"  "+"Sequence: "+str(item.get('Sequence')))

打印:

ECF02  pSymA  -  Validated results: Total number of hits: 284

Start: 2876  End: 2903  Strand: +  Sequence: GAACTACAGGAAGTTCATGGTCTCCTTC
Start: 2876  End: 2900  Strand: +  Sequence: GAACTACAGGAAGTTCATGGTCTCC
Start: 9214  End: 9238  Strand: +  Sequence: GAACGTCGGGCGCAAGAGCCGCTTT
...

请注意:dictList 已被 finalList 取代。由于我的部分脚本在这些打印之前运行

【问题讨论】:

    标签: python-3.x list dictionary output


    【解决方案1】:

    您可以使用 .format_map(each_dic) 或更高版本的 Python 3.5+ 添加替代 .format(**each_dic)。在每个打印中使用列表中的每个 dict 格式。

    for each_dic in dictList:
        print('Sequence: {Sequence:<29} '
              'Start: {Start:>7} '
              'Strand: {Strand} '
              'End: {End:>7}'.format_map(each_dic))
    

    查看 Python 帮助页面:string - Common string operations 以获得更多参考和示例。

    【讨论】:

      【解决方案2】:

      通过简单地指定结尾来避免换行:

      print("Start"+item.get('Start'), end=" ")
      

      end=" " 将继续您的字符串而无需换行

      【讨论】:

      • 啊,所以我只是想,我也可以这样做: print("Start"+item.get('Start)'+" "+"End"+item.get('End' )...等)并将其全部写在一个大字声明中。那么,新的印刷品换行这一事实将很有用
      【解决方案3】:

      取自我的更新,灵感来自 Aleksey Solovey

      print(ecf+"  "+replicon+"  -  "+"Validated results: Total number of hits: "+str(len(finalList)))
      print("")
      
      for item in finalList:
          print("Start: "+str(item.get('Start'))+"  "+"End: "+str(item.get('End'))+"  "+"Strand: "+str(item.get('Strand'))+"  "+"Sequence: "+str(item.get('Sequence')))
      

      【讨论】:

        猜你喜欢
        • 2020-11-10
        • 2016-11-26
        • 2020-10-23
        • 2016-01-30
        • 2015-01-12
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        相关资源
        最近更新 更多