Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python 3编写非文本文件_Python_Image_Python 3.x_Encryption_Encoding - Fatal编程技术网

使用Python 3编写非文本文件

使用Python 3编写非文本文件,python,image,python-3.x,encryption,encoding,Python,Image,Python 3.x,Encryption,Encoding,我目前正在开发一个加密程序,面临着用Python 3编写非文本文件的问题 例如,这将起作用(定义了fEncode()): 此处text文件名='C:\aRandomTextFile.txt'。但是,如果我替换它,将使用类似于“C:\aRandomImage.png”的东西替换它 ciphertextFile = open(textFileName, 'w', encoding='utf-8') 与 然后试试看 ciphertextFile.write(bytes(str(ciphertex

我目前正在开发一个加密程序,面临着用Python 3编写非文本文件的问题

例如,这将起作用(定义了fEncode()):

此处text文件名='C:\aRandomTextFile.txt'。但是,如果我替换它,将使用类似于“C:\aRandomImage.png”的东西替换它

 ciphertextFile = open(textFileName, 'w', encoding='utf-8')

然后试试看

 ciphertextFile.write(bytes(str(ciphertext, encoding='utf-8')))
我明白了

回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第685行,在runfile中
execfile(文件名、命名空间)
文件“C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第85行,在execfile中
exec(编译(打开(文件名'rb').read(),文件名'exec'),命名空间)
文件“C:/Comp_Sci/Coding/chr_ord_5.py”,第466行,in
write(字节(str(ciphertext,encoding='utf-8'))
TypeError:不支持解码str

我做错了什么?

问题在于如何使字节字符串化。考虑下面的内容,类似于你正在做的:

>>> bytes('banana')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    bytes('banana')
TypeError: string argument without an encoding

甚至只是:

>>> 'banana'.encode()
b'banana'
现在你可以看到
bytes(str('banana',encoding='utf-8'))
正在获取一个字符串,将其转换为二进制字符串,将其转换为未编码字符串,然后再次尝试将其转换为bytes字符串


希望对您有所帮助。

您是否尝试过str.encode(密文,encoding='utf-8')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
  File "C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  File "C:/Comp_Sci/Coding/chr_ord_5.py", line 466, in <module>
ciphertextFile.write(bytes(str(ciphertext, encoding='utf-8')))
TypeError: decoding str is not supported
>>> bytes('banana')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    bytes('banana')
TypeError: string argument without an encoding
>>> bytes(str('banana',encoding='utf-8'))
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    bytes(str('banana',encoding='utf-8'))
TypeError: decoding str is not supported
>>> bytes('banana'.encode('utf-8'))  # redundant, see last example
b'banana'
>>> bytes(ord(c) for c in 'banana')
b'banana'
>>> 'banana'.encode()
b'banana'