gpt4 book ai didi

python - 无法将字节连接到 str

转载 作者:IT老高 更新时间:2023-10-28 22:22:03 26 4
gpt4 key购买 nike

这被证明是向 python 的粗略过渡。这是怎么回事?:

f = open( 'myfile', 'a+' )
f.write('test string' + '\n')

key = "pass:hello"
plaintext = subprocess.check_output(['openssl', 'aes-128-cbc', '-d', '-in', test, '-base64', '-pass', key])
print (plaintext)

f.write (plaintext + '\n')
f.close()

输出文件如下所示:

测试字符串

然后我得到这个错误:

b'decryption successful\n'
Traceback (most recent call last):
File ".../Project.py", line 36, in <module>
f.write (plaintext + '\n')
TypeError: can't concat bytes to str

最佳答案

subprocess.check_output() 返回一个字节串。

在 Python 3 中,unicode (str) 对象和 bytes 对象之间没有隐式转换。如果你知道输出的编码,你可以.decode()得到一个字符串,也可以把你要添加的\n转成字节"\n".encode('ascii')

关于python - 无法将字节连接到 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21916888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com