python - 需要在 python 中使用字符串索引的帮助

标签 python indexing

当我尝试使用索引来获取行的最后一个元素时,它不起作用(打印出一个空行)。但是,当我打印原始行时,它可以工作。

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

When index is used When index is not used

最佳答案

原因可能是该行末尾有换行符。如果该行的最后一个元素是“\n”,因为文件有多行,那么它肯定会打印一个空行。

关于python - 需要在 python 中使用字符串索引的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66620035/

相关文章:

python - 如何正确地将字节对象传递给 Python 中的新线程?

sql - 如何检查 SQL 命令是否仍在运行?还是它被卡住/死了……?

python - 在 numpy 中使用列表和数组进行索引似乎不一致

c++ - 枚举和索引所有可能的 n 个顶点的树

php - 确定 PHP 数组是否使用键或索引

python - 在已部署的Flask应用程序(Heroku)上使用OpenCV

python - 从基于另一个列表的列表列中删除列表值

Python "in"range() 上的运算符时间复杂度

python - 将 `column` 添加到 geoDataFrame.plot 会出现 TypeError

indexing - HBase 访问和索引