当我尝试使用索引来获取行的最后一个元素时,它不起作用(打印出一个空行)。但是,当我打印原始行时,它可以工作。
with open( newFile,"w") as f:
f.write("Model Number One" + "\n")
for l in lines:
if "ATOM" in l :
f.write(l[-1]) #Does NOT work, prints empty line
f.write(l) # Prints the whole linem when I only want the last element of the line
最佳答案
原因可能是该行末尾有换行符。如果该行的最后一个元素是“\n”,因为文件有多行,那么它肯定会打印一个空行。
关于python - 需要在 python 中使用字符串索引的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66620035/