我正在尝试格式化文档。无论是否有信息,每行都需要长158个空间。我得到了需要正确的输出,但它在之间打印了其他线条。
示例:我应该期望看到,
1
2
3
4
我得到:
1
2
3
4
我尝试了rstrip((,这删除了其他空白行,但也删除了我需要格式化的空间。
for line in f1:
#removes leading white spaces
line.strip()
#finds the length of the line
x = (len(line))
#hard set value for the type of document
y = 158
#finding the difference between string length and hard value
z = (y - x)
#prints the difference and string length
Str1 = line.ljust(z)
print (Str1)
我希望每行的长度正好158个字符,并且每行之间的空间为零。目前,我可以获得空格或158个字符线,而不是两者。
for line in f1:
#removes leading white spaces
line.strip()
#finds the length of the line
x = (len(line))
#hard set value for the type of document
y = 158
#finding the difference between string length and hard value
z = (y - x)
#prints the difference and string length
Str1 = line.ljust(z)
print (Str1,end="")
end ="不会再创建一个" n",您的输出将很好。
您可以尝试:
line.replace("nn", "n")