Python: List Of Dictionaries: How To - Nice Printout Using Sys.stdout
I have the following list of dictionaries: [{'Sequence': 'TGACCCTGCTTGGCGATCCCGGCGTTTC', 'Start': '52037', 'Strand': '+', 'End': '52064'}, {'Sequence': 'TGATCGCGCAACTGCAGCGGGAGTTAC
Solution 1:
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))
Look at Python help page: string - Common string operations for further reference and examples.
Solution 2:
Avoid new lines by simply specifying the ending with:
print("Start"+item.get('Start'), end=" ")
end=" "
will continue your string without new lines
Solution 3:
Taken from my update, inspired by 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')))
Post a Comment for "Python: List Of Dictionaries: How To - Nice Printout Using Sys.stdout"