使用正确的打印格式从字典中循环



我有这个字典,我需要将它以类似表格的方式格式化为文本文件。

dictionary_lines = {'1-2': (69.18217255912117, 182.95794152905918), '2-3': (35.825144800822954, 175.40503498180715), '3-4': (37.34332738254673, 97.30771061242511), '4-5': (57.026590289091914, 97.33437880141743), '5-6': (57.23912298419586, 14.32271997820363), '6-7': (55.61382561917492, 351.4794228420951), '7-8': (41.21551406933976, 275.1365340619268), '8-1': (57.83213034291623, 272.6560961904868)}

到目前为止,我正在先把它们打印出来,然后再写一个新文件。我一直纠结于如何正确格式化它们。到目前为止,我得到的是:

for item in dictionary_lines:
print ("{l}t{d:<10.3f}t{a:<10.3f}".format(l= ,d =,a = ))

我想把它打印成这样:

Lines[tab] Distances [tab]  Azimuth from the South
Key 0       value1 in tuple0  Value2 in tuple0

format()中不使用字典中的数据。

此外,考虑使用items()迭代字典的键值对,如下所示:

for key, value in dictionary_lines.items():
print ("l={}td={:<10.3f}ta={:<10.3f}".format(key, value[0], value[1]))
l=1-2   d=69.182        a=182.958   
l=2-3   d=35.825        a=175.405   
l=3-4   d=37.343        a=97.308    
l=4-5   d=57.027        a=97.334    
l=5-6   d=57.239        a=14.323    
l=6-7   d=55.614        a=351.479   
l=7-8   d=41.216        a=275.137   
l=8-1   d=57.832        a=272.656  

最新更新