How do you align text in a text file in python?

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

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

I'm trying to print the data in left- and right-aligned columns:

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

How can I do this?

This is the code I have so far:

with open(r'C:\path\to\logons.txt', 'r') as f:
    for line in f:
        data = line.strip()
        print(data)

tdy

29.4k10 gold badges52 silver badges56 bronze badges

asked May 28, 2013 at 16:09

I would go for the new(er) print formatter with this one (assuming your fields are consistent). The print/format statement is pretty easy to use and can be found here. Since your data can be seen as a list, you can do a single call to format and supplying the correct formatter data you'll get your output. This has a bit more fine grained control than ljust or rjust but has the downside that you need to know that your data coming in is consistent.

with open(r'C:\path\to\logons.txt', 'r') as f:
    for line in f:
        data = line.split()    # Splits on whitespace
        print '{0[0]:<15}{0[1]:<15}{0[2]:<5}{0[3]:<15}{0[4]:>15}'.format(data)

answered May 28, 2013 at 17:21

How do you align text in a text file in python?

Jeff LangemeierJeff Langemeier

9881 gold badge11 silver badges21 bronze badges

0

str.ljust(width, [fillchar=" "]) (http://docs.python.org/2/library/stdtypes.html#str.ljust) seems like what you're after. Left justify each field when printing to the maximum length + a little bit.

For the last field to match your example, you'll want to right justify it instead using rjust.

answered May 28, 2013 at 16:21

SysyphusSysyphus

1,0715 silver badges10 bronze badges

5

As of Python 3.6+

Use the modern f-string syntax:

with open(r'logons.txt', 'r') as f:
    for line in f:
        s = line.split()
        print(f'{s[0]:<10}{s[1]:<17}{s[2]:<5}{s[3]:<12}{s[4]:>12}')
        #     ^       ^^^       ^^^       ^^       ^^^       ^^^
        # f-string  left-10   left-17   left-5   left-12   right-12
  • much faster than the older str.format
  • follows the same format specifications

Output:

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

answered Dec 5, 2021 at 23:10

tdytdy

29.4k10 gold badges52 silver badges56 bronze badges

#!/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')

answered May 28, 2013 at 17:14

Hal CanaryHal Canary

2,03416 silver badges17 bronze badges

How do you align text in Python?

You can use the :> , :< or :^ option in the f-format to left align, right align or center align the text that you want to format. We can use the fortmat() string function in python to output the desired text in the order we want.

How do you structure a text file in Python?

There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character ('\n') in python by default.

How can we align the text?

For example, in a paragraph that is left-aligned (the most common alignment), text is aligned with the left margin. In a paragraph that is justified, text is aligned with both margins. ... Align text left, center, or right..

How do you align text in a column in Python?

Use the just() Function to Print With Column Alignment in Python. In Python, we have different methods for string alignment. We can align the strings using the ljust() , rjust , and center() functions. Using these, we can align the rows appropriately based on our requirements.