我有这本字典,我需要它以表格的方式格式化成文本文件。
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发布于 2021-03-26 17:48:03
您不会在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 https://stackoverflow.com/questions/66814388
复制相似问题