我需要使用python将数据打印为结构

时间:2017-08-31 08:49:28

标签: python

数据:[(Taru,1234ABCD,4536,EF32),(Aarul,10045660,4562,ABDE),(Vinay,1254EFDC,2587,AC42],以列表形式 输出应该像(表格形式)

回复:Taru 1234ABCD
                4536
                EF32

     Aarul  10045660   
            4562
            ABDE  

     Vinay  1254EFDC
            2587
            AC42

请提供您的输入以解决此问题。谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用这个小脚本:

l = [['Taru', '12345678ABCDEF', 453678], ['Aarul', '10045660ABDECABF', 45621278]]

print("HEADER1       HEADER2             HEADER3")
for ele1,ele2,ele3 in l:
    print("{:<14}{:<11}{:13}".format(ele1,ele2,ele3))

结果:

HEADER1       HEADER2             HEADER3
Taru          12345678ABCDEF       453678
Aarul         10045660ABDECABF     45621278

答案 1 :(得分:0)

我认为你的主要问题是如何分割你得到的清单?这似乎是这样做的模式

编辑根据评论主要是格式化,这是一种可能的解决方案

entries = [["Taru", "1234ABCD", "4536", "EF32"], ["Aarul", "10045660", "4562", "ABDE"], ["Vinay", "1254EFDC", "2587", "AC42"]]

csv = 'Name,information\n'   
# this has split your array into the parts you want
for entry in entries:
    left = entry[0]
    for word in entry[1:]:
        print("{:<10}{:<10}".format(left,word))
        csv += str(left) + ',' + str(word) + '\n'
        left = ''
    print()

with open('output.csv', 'w') as file:
    file.write(csv)

OUPUT:

Taru      1234ABCD  
          4536      
          EF32      

Aarul     10045660  
          4562      
          ABDE      

Vinay     1254EFDC  
          2587      
          AC42

ouput.csv:

Name,information
Taru,1234ABCD
,4536
,EF32
Aarul,10045660
,4562
,ABDE
Vinay,1254EFDC
,2587
,AC42