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()
我会在python上获取我在这里的数据,并在写入时将其垂直对齐到文件中。最有效的方法是什么?
答案 0 :(得分:2)
我认为最有效的方法是循环遍历字符串,然后写入文件:
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()
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