Created
December 7, 2016 08:13
-
-
Save ricdeez/af00826859b9e0ac96d0ab70d99b96e0 to your computer and use it in GitHub Desktop.
Padding pipe delimited text for inclusion in an ascii file nicelly formatted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
def pad_left(n, width, pad=" "): | |
return ((pad * width) + str(n))[-width:] | |
data = """ | |
SERVER|IP_ADDR|LOCATION|PCN | |
NH-SRV01|10.97.9.30|SUB704|HUB | |
NH-SRV05|10.97.9.34||HUB | |
NH-SRV07|10.97.9.40|SUB501|HUB | |
NH-SRV11|10.97.9.44|SUB501|HUB | |
IRONNEW-APP03|134.18.86.55|Bene Server room|BENE | |
Ironnew-FSR01|134.18.86.56|Bene Server room|BENE | |
""".splitlines() | |
data = [x.split('|') for x in data if len(x) > 0] | |
m1 = max(len(str(x[0])) for x in data) | |
m2 = max(len(str(x[1])) for x in data) | |
m3 = max(len(str(x[2])) for x in data) | |
m4 = max(len(str(x[3])) for x in data) | |
new_data = [] | |
for x,y,z,w in data: | |
x = pad_left(x, m1) | |
y = pad_left(y, m2) | |
z = pad_left(z, m3) | |
w = pad_left(w, m4) | |
new_data.append([x, y, z, w]) | |
for entry in new_data: | |
print "|".join(entry) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment