starrynights89
starrynights89

Reputation: 139

How do I align data vertically in python?

def createFile(n):
filename = "myFile.txt"
outputFile = open(filename, "w")
outputFile.write("WWWW 2 77777 54 M 888 90 6.7 100 No yyy kk 888 zz F too yy 8.8 123 xxx yyy pp")
outputFile.close()

I would take the data I have here on python, and align it vertically to a file when I write it. What would be the most efficient way to do this?

Upvotes: 1

Views: 519

Answers (1)

user1786283
user1786283

Reputation:

I believe the most efficient way is looping through the string and then writing to the file:

filename = "myFile.txt"
outputFile = open(filename, "w")
string = "WWWW 2 77777 54 M 888 90 6.7 100 No yyy kk 888 zz F too yy 8.8 123 xxx yyy pp"
outputFile.write("\n".join(item for item in string.split(" ")))
outputFile.close()

Output in myFile.txt:

WWWW
2
77777
54
M
888
90
6.7
100
No
yyy
kk
888
zz
F
too
yy
8.8
123
xxx
yyy
pp

Upvotes: 1

Related Questions