【问题标题】:Python print() and sys.stdout.write() issuesPython print() 和 sys.stdout.write() 问题
【发布时间】:2017-11-16 13:12:06
【问题描述】:

大家可以在 python 2.7 中看到下面的代码。 我遇到了代码的“print(ciphertext)”和“sys.stdout.write(ciphertext)”部分的问题 当我运行代码时,“print(passline)”和“sys.stdout.write(passline)”出来的很好,即如果文件有一行写着“Billz”,它将按原样显示,但是当我尝试使用输出时任一函数(即 sys.stdout.write() 和 print())密文(通过 encryptMessage(key, message) 方法) 输出根据“myKey”变量跨行拆分(代码和示例见下文)

*我了解转置加密方法的局限性,但是'密文'会在原行完成输出之前从它开始的行开始换行

我认为问题在于 encryptMessage() 函数以及它如何与 enc() 方法交互,即特别是代码的 for ...in... 块

这有意义吗?

我认为这个问题的答案可以在以下情况下提供帮助 - 从文件中读取数据但不覆盖这些文件 - 尝试编写与日志、密码/单词列表相关的程序时 - 并了解 for、in 和 .join 如何协同工作

i.e. myKey = 1
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 1
This is a list of Passwords to be encrypted

This is a list of Passwords to be encrypted

Billz786

Billz786

123456

123456

Milly

Milly

Bilklzcfvcx
Bilklzcfvcx

i.e. myKey = 2

C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 2
This is a list of Passwords to be encrypted

Ti sals fPswrst eecytdhsi  ito asod ob nrpe

Billz786

Blz8
il76
123456

135
246
Milly

Mlyil

Bilklzcfvcx
Bllcvxikzfc

i.e. myKey = 4

C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 4
This is a list of Passwords to be encrypted

T asfsrtecthi t sdo reisl Pws eyds ioao bnp

Billz786

Bz
i7l8l6
123456

15263
4
Milly

Myi
ll
Bilklzcfvcx
Blvizclcxkf

i.e. myKey = 8
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 8
This is a list of Passwords to be encrypted

Tafreth  d eilPsedsia n
 sstcitsors w y oobp
Billz786

B
illz786
123456

123456

Milly

Milly

Bilklzcfvcx
Bviclxklzcf

代码是

def enc():
    myMessage = raw_input('Enter file name: ')
    myKey = int(raw_input('Enter Key: '))
    text_file = open(myMessage, "r")
    lines = text_file.readlines()
    for passline in lines:
        myMessage = passline
        ciphertext = encryptMessage(myKey, myMessage)
        print(passline)
        #sys.stdout.write(passline)
        print ciphertext
        #sys.stdout.write(ciphertext)
    text_file.close()
def encryptMessage(key, message):
    ciphertext = [''] * key
    for col in range(key):
        pointer = col
        while pointer < len(message):
            ciphertext[col] += message[pointer]
            pointer += key
    return ''.join(ciphertext)

【问题讨论】:

    标签: python-2.7 printing output


    【解决方案1】:

    当使用readlines 和大多数其他读取文件行的​​方式时,python 在行中包含换行符(因此,在您的情况下,passline 包含(a)换行符)。为防止这种情况,请在 for 循环的开头使用 passline = passline.rstrip('\n\r') 之类的内容

    【讨论】:

    • 完美我的朋友我现在得到了我想要的方式,即在单独的行中未加密和加密,没有单独的行拆分:) :) TOP MAN - 仅供参考,其他人在下面插入了上述代码“for passline in lines:”按照python语法缩进:D
    • 这是要加密的密码列表 Tss soPsr cpdh ltfawdtbertiiiai sosoenye Billz786 Bl8iz6l7 123456 142536 Milly Mliyl Bilklzcfvcx Bkccilfxlzv
    猜你喜欢
    • 2011-03-16
    • 2011-12-22
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多