问题描述
我有以下词典列表:
[{'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重定向打印):
I would like to acquire the following output (redirecting prints via sys.stdout):
Start:1234 End:5678 Strand:+ Sequence:ABCDEFG
...
到目前为止,我的想法是循环遍历列表,并使用 get-method 获取键的值,然后进行手动打印,基本上是这样的:
So far, my idea involves looping through the list and using the get-method to get the value of the key and then do a manual printout, basically like this:
for item in dictList:
print("Start"+item.get('Start'))
print("End"+item.get('End'))
...
我不确定如何处理换行符.我读了有关sys.stdout刷新的内容,以避免换行符,但是由于我不想避免所有换行符,因此不确定是否符合我的目标.
I am not sure how to deal with the newlines though. I read about sys.stdout flush to avoidnewlines, but since I do not want to avoid all newlines, I am not sure if this would suit my aim.
有更聪明/更短的方法吗?
Is there a smarter/shorter way to do this?
感谢Aleksey,我想将我的印刷声明更改为:
Thanks to Aleksey, I thought of changing my print statement to:
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')))
Update2
到目前为止,我坚持这样做:
Update2
so far, I am sticking to this:
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取代.由于我的脚本的某些部分在打印之前就已运行
please note: dictList was replaced by finalList. Due to parts of my script running before those prints
推荐答案
您可以使用 .format_map(each_dic)
或更高版本的Python 3.5+添加替代方法 .format(** each_dic)
.使用每种格式的打印列表中的每个字典.
You could use .format_map(each_dic)
or later Python 3.5+ added alternative .format(**each_dic)
. Uses each dict in the list in each print with formatting.
for each_dic in dictList:
print('Sequence: {Sequence:<29} '
'Start: {Start:>7} '
'Strand: {Strand} '
'End: {End:>7}'.format_map(each_dic))
查看Python帮助页面:字符串-常见的字符串操作参考和示例.
Look at Python help page: string - Common string operations for further reference and examples.
这篇关于Python:词典列表:如何使用sys.stdout进行漂亮的打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!