python去掉最后一行的换行符_如何在Python中删除文本中的最后一个换行符?

我有一个程序,解码一个凯撒密码和几个文本文件与多行解码。在

根据我的讲师代码检查器,文本后面总是有一个空白行,但是当我自己运行代码时,我看不到任何东西。在

删除最后一个字符只会删除文本中的最后一个字母或数字,而不会删除换行符。在

我的代码是:import sys

import string

import collections

ciphertext_lines = sys.stdin.readlines()

ciphertext = ''

for i in ciphertext_lines:

ciphertext += i

alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

def getShiftVal():

string = ''

for line in ciphertext:

string = string + line

most_common_letter = ((collections.Counter(string).most_common(2)[1])[0])

shift_val = (alphanum.index(most_common_letter) - 4)

return shift_val

def decrypt(ciphertext, n):

alphabet_numbers = collections.deque(string.ascii_uppercase + string.digits)

alphanum = ''.join(list(alphabet_numbers))

alphabet_numbers.rotate(n)

alphanum_rotated = ''.join(list(alphabet_numbers))

return ciphertext.translate(str.maketrans(alphanum, alphanum_rotated))

def main():

n = getShiftVal()

decrypted = decrypt(ciphertext, n)

print(decrypted)

if __name__ == '__main__':

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值