这是使用numpy 模块加上UTF-32 编码/解码的代码。此代码对于大数据将非常快,并且不需要 Python 循环。
numpy 模块可以使用python -m pip install numpy 轻松安装。如果您需要没有 numpy 的解决方案,使用纯 Python,并且运行速度不是问题,请告诉我,我会重写,但纯 Python 代码在大数据上的运行速度会慢得多。
你也可以run this code online here。
# Needs: python -m pip install numpy
import numpy as np
word = 'Duck'
key = 1
a = np.frombuffer(word.encode('utf-32-le'), dtype = np.int32)
a = a - key
a[a < 97] += 26
text = a.tobytes().decode('utf-32-le')
print(text)
还有类似的较慢的下一个解决方案,但没有numpy,只使用标准Python 的内置模块struct。你也可以run next code online。
import struct
word = 'Duck'
key = 1
text = ''
for i in word:
c = struct.unpack('<I', i.encode('utf-32-le'))[0] - int(key)
if c < 97:
c = c + 26
b = struct.pack('<I', c).decode('utf-32-le')
text += b
print(text)
下面的另一个解决方案不使用任何模块。 Run next code online.
word = 'Duck'
key = 1
text = ''
for i in word:
c = int(i.encode('utf-32-be').hex(), 16) - int(key)
if c < 97:
c = c + 26
b = bytes.fromhex(hex(c)[2:].zfill(8)).decode('utf-32-be')
text += b
print(text)
如果文本符号仅来自 ASCII 集合,则代码可以进一步简化 (run this code online):
word = 'Duck'
key = 1
text = ''
for i in word:
c = i.encode('ascii')[0] - int(key)
if c < 97:
c = c + 26
b = bytes((c,)).decode('ascii')
text += b
print(text)
另一种解决 ASCII 字符情况的方法,使用两个表 (run this code online)
word = 'Duck'
key = 1
tmp = [(c, i) for i, c in enumerate(bytes(range(128)).decode('ascii'))]
c2i = dict(tmp)
i2c = [e[0] for e in tmp]
text = ''
for i in word:
c = c2i[i] - int(key)
if c < 97:
c = c + 26
b = i2c[c]
text += b
print(text)
通过替换下一行 (run this code online),可以将以前的代码从 ASCII 扩展到更宽的字符集(例如 16 位):
tmp = [(bytes.fromhex(hex(i)[2:].zfill(8)).decode('utf-32-be', 'replace'), i) for i in range(1 << 16)]