【问题标题】:Extra blank line is getting printed at the end of the output in Python在 Python 的输出末尾打印出额外的空白行
【发布时间】:2021-11-11 14:56:46
【问题描述】:

我正在尝试从命令行读取文件,并尝试在该文件中使用空白 replace all the commas。以下是我的代码:

import sys
datafile = sys.argv[1];

with open(datafile, 'r') as data:
  plaintext = data.read()

plaintext = plaintext.replace(',', '')
print(plaintext)

但是在打印明文时,我得到了one extra blank row at the end。为什么会发生这种情况,我该如何摆脱它?

【问题讨论】:

    标签: python python-3.x file


    【解决方案1】:

    你也许可以使用

    plaintext.rstrip('\n') 
    

    这应该删除多余的行

    【讨论】:

    • 谢谢。它工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多