簡體   English   中英

在 Python 輸出的末尾打印額外的空行

[英]Extra blank line is getting printed at the end of the output in Python

我正在嘗試從命令行讀取文件並嘗試用空白replace all the commas該文件中的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 為什么會發生這種情況,我該如何擺脫它?

你也許可以使用

plaintext.rstrip('\n') 

這應該刪除額外的行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM