Align columns in a text file

前端 未结 3 1421
北海茫月
北海茫月 2021-02-05 10:03

I am a Python noobie and I\'m stuck on something I know is going to be simple...

I have a plain text file that contains user login data:

dtrapani  HCPD-E         


        
3条回答
  •  情书的邮戳
    2021-02-05 10:56

    #!/usr/bin/env python
    import sys
    inputfile = '''dtrapani HCPD-EPD-3687 Mon 05/13/2013 9:47:01.72
    dlibby HCPD-COS-4611 Mon 05/13/2013 9:49:34.55
    lmurdoch HCPD-SDDEB-3736 Mon 05/13/2013 9:50:38.48
    lpatrick HCPD-WIN7-015 Mon 05/13/2013 9:57:44.57
    mlay HCPD-WAR-3744 Mon 05/13/2013 10:00:07.94
    eyoung HCPD-NLCC-0645 Mon 05/13/2013 10:03:01.83'''.split('\n')
    output = sys.stdout
    lengths = [10,17,5,14,0]
    for line in inputfile:
        line = line.split()
        for field, fieldlength in zip(line,lengths):
            output.write(field.ljust(fieldlength))
        output.write('\n')
    

提交回复
热议问题