Python: 将文本文件转换为二进制文件
Python: Converting a Text File to a Binary File
我们可以将任何数字文件转换成二进制文件。
我有一个 1MB 的文本文件,
我想将其转换为二进制字符串并将输出视为二进制数,反之亦然,
换句话说,如果我有二进制数,我想把它转换成文本文件。
我怎么能在 Python 中做到这一点?有没有标准的方法来做到这一点?
现在本论坛有一些帖子(1,3, 4
) 但是 none 他们正确回答了我的问题。
请参阅 https://docs.python.org/3/library/codecs.html#standard-encodings 以获取标准字符串编码列表,因为转换取决于编码。
这些函数将有助于在 bytes/ints 和字符串之间进行转换,默认为 UTF-8。
提供的示例使用 UTF-8 中的韩文字符“한”。
def bytes_to_string(byte_or_int_value, encoding='utf-8') -> str:
if isinstance(byte_or_int_value, bytes):
return byte_or_int_value.decode(encoding)
if isinstance(byte_or_int_value, int):
return chr(byte_or_int_value).encode(encoding).decode(encoding)
else:
raise ValueError('Error: Input must be a bytes or int type')
def string_to_bytes(string_value, encoding='utf-8') -> bytes:
if isinstance(string_value, str):
return bytes(string_value.encode(encoding))
else:
raise ValueError('Error: Input must be a string type')
int_value = 54620
bytes_value = b'\xED\x95\x9C'
string_value = '한'
assert bytes_to_string(int_value) == string_value
assert bytes_to_string(bytes_value) == string_value
assert string_to_bytes(string_value) == bytes_value
提到的“文本文件”似乎是指ASCII文件。 (其中每个字符占用 space 的 8 位)。
第 2 行“将其转换为二进制字符串”可能意味着文本文件的 ASCII 表示形式,将字节序列“输出为二进制数”(类似于 public 密钥加密,其中 text is converted to a number 加密前)例如
text = 'ABC '
for x in text:
print(format(ord(x), '08b'), end='')
会给出二进制(数字)字符串:01000001010000100100001100100000
十进制为:1094861600
第 3 行表示(字节)对二进制数进行排序并显示等效的 ASCII 字符(对于每个 8 位序列),例如。 0x41 将替换为 'A'(作为输出)(这里的假设是每个数字将映射到可打印的 ASCII,即文本字符,并且给定的二进制数是 8 位数字的倍数)。
例如。反转(将二进制数转换为文本):
binary = "01000001010000100100001100100001"
#number of characters in text
num = len(binary)/8
for x in range(int(num)):
start = x*8
end = (x+1)*8
print (chr(int(str(binary[start:end]),2)), end='')
print()
会给出文本:ABC!
对于 1MB 的文本文件,您可以将文本字符串拆分为您的机器可以处理的块,例如。 32 位(转换前)
中测试
我们可以将任何数字文件转换成二进制文件。
我有一个 1MB 的文本文件,
我想将其转换为二进制字符串并将输出视为二进制数,反之亦然,
换句话说,如果我有二进制数,我想把它转换成文本文件。
我怎么能在 Python 中做到这一点?有没有标准的方法来做到这一点?
现在本论坛有一些帖子(1,
请参阅 https://docs.python.org/3/library/codecs.html#standard-encodings 以获取标准字符串编码列表,因为转换取决于编码。
这些函数将有助于在 bytes/ints 和字符串之间进行转换,默认为 UTF-8。
提供的示例使用 UTF-8 中的韩文字符“한”。
def bytes_to_string(byte_or_int_value, encoding='utf-8') -> str:
if isinstance(byte_or_int_value, bytes):
return byte_or_int_value.decode(encoding)
if isinstance(byte_or_int_value, int):
return chr(byte_or_int_value).encode(encoding).decode(encoding)
else:
raise ValueError('Error: Input must be a bytes or int type')
def string_to_bytes(string_value, encoding='utf-8') -> bytes:
if isinstance(string_value, str):
return bytes(string_value.encode(encoding))
else:
raise ValueError('Error: Input must be a string type')
int_value = 54620
bytes_value = b'\xED\x95\x9C'
string_value = '한'
assert bytes_to_string(int_value) == string_value
assert bytes_to_string(bytes_value) == string_value
assert string_to_bytes(string_value) == bytes_value
提到的“文本文件”似乎是指ASCII文件。 (其中每个字符占用 space 的 8 位)。
第 2 行“将其转换为二进制字符串”可能意味着文本文件的 ASCII 表示形式,将字节序列“输出为二进制数”(类似于 public 密钥加密,其中 text is converted to a number 加密前)例如
text = 'ABC '
for x in text:
print(format(ord(x), '08b'), end='')
会给出二进制(数字)字符串:01000001010000100100001100100000
十进制为:1094861600
第 3 行表示(字节)对二进制数进行排序并显示等效的 ASCII 字符(对于每个 8 位序列),例如。 0x41 将替换为 'A'(作为输出)(这里的假设是每个数字将映射到可打印的 ASCII,即文本字符,并且给定的二进制数是 8 位数字的倍数)。
例如。反转(将二进制数转换为文本):
binary = "01000001010000100100001100100001"
#number of characters in text
num = len(binary)/8
for x in range(int(num)):
start = x*8
end = (x+1)*8
print (chr(int(str(binary[start:end]),2)), end='')
print()
会给出文本:ABC!
对于 1MB 的文本文件,您可以将文本字符串拆分为您的机器可以处理的块,例如。 32 位(转换前)