用十六进制值替换字符串中的`{}`

问题描述 投票:0回答:2

我有一个随机内容的字符串,例如:

random_string = """

asdasdasd23123asdnos{}n
asdnoiaf{}aosndiasdasd
asd{}1239i123nig
hpodpfh{}askmdiasd
{asdasda
}asdasdasd
"""

我想用随机十六进制值替换该字符串中每次出现的{},该值与替换其他花括号的先前值不同。

我正在使用此函数生成100%随机的十六进制值。

def get_random(n):
    out = set()
    while len(out) < n:
        out.add(binascii.b2a_hex(os.urandom(1)))
    return list(out)

我缺少一个函数或循环,其中,十六进制的值取自列表get_random(n),将字符串中的{}替换为列表中十六进制的值并打印输出,如果有任何字符在{},他们应该被跳过。

我使用的是模块re,但并非所有值都是随机的,很有可能某些值是相似的。

print(re.sub('{}', lambda _: binascii.b2a_hex(os.urandom(1)) + ' \\', random_string))

任何帮助表示赞赏!

python regex string python-2.7 hex
2个回答
2
投票

我认为你这太复杂了。函数b2a_hex生成2位十六进制字符串。由于这只是256(0-> 255)元素,只需生成所有元素,随机播放并生成它们直到它们消失为止:

def unique_hex():
    all_hex=['{:02x}'.format(e) for e in range(256)]
    random.shuffle(all_hex)
    for e in all_hex:
        yield e
    # returns None at this point. Add what you think it should do with no more

您可以看到每次调用它是00ff之间所有唯一十六进制数字的随机列表:

>>> list(unique_hex())
['95', '6a', '23', '1e', 'cb', '30', '78', '0f', 'e3', 'b2', '2d', '5c', '72', '7b', '9e', 'bd', '76', 'd8', '43', '6b', '0a', 'c6', '90', '0b', 'bf', 'a6', 'bc', 'd3', 'f3', '54', '15', 'b4', 'e9', '4a', 'b7', 'd5', 'cc', '2f', '00', 'f1', '64', 'f0', '2e', 'c0', 'b9', '9b', '7e', 'a9', '01', '03', '06', '27', 'b6', 'ab', '8d', '4b', '4d', 'f4', '19', 'df', '89', 'b0', '40', 'b1', '37', 'e2', '68', '5f', '39', 'ed', '16', 'a0', '5d', 'c3', 'a3', '1a', '13', '45', 'ce', 'c9', 'a5', '63', '60', '62', '75', '41', 'c4', '52', 'd2', 'cf', '61', '98', 'f9', 'c7', '18', 'ba', '34', '8c', '79', '81', '56', 'ff', 'd6', 'f8', '59', 'e7', '25', 'be', 'de', 'd1', '21', 'f7', '7c', '92', '67', '9a', '85', 'fd', 'fa', '07', '02', '49', '51', '87', '97', '38', '8a', 'b3', 'fe', 'da', '14', 'f6', '93', '8f', 'f5', '6c', '3d', 'c1', '70', '82', '55', 'ac', 'ae', '48', '11', '9c', 'ee', '1d', 'a2', '05', '6f', '71', '1f', 'ec', '58', 'ea', '9d', '8b', '0d', '83', '22', '4e', '66', 'cd', 'a1', 'ad', '3b', '88', '44', '53', 'e6', '3f', '47', 'fb', 'a4', '69', 'e8', '6d', '04', '7a', '20', '29', '84', 'af', '0c', 'c8', 'e4', '33', '74', 'ca', '65', 'c5', 'd4', '7d', '5a', 'f2', '96', '5b', 'e0', '32', '3e', '0e', 'a8', 'b5', '28', '36', '80', '08', 'a7', '09', '5e', '77', '3c', '46', '73', '91', '2a', '17', '99', 'fc', 'eb', '50', 'd0', '3a', '94', '24', '57', 'dc', '26', 'ef', '1b', 'd9', 'aa', '31', 'e1', '42', '1c', '2c', 'bb', '6e', 'db', 'c2', 'b8', '86', '9f', '12', '4f', 'd7', '4c', '2b', '10', '35', 'e5', '7f', 'dd', '8e']
>>> list(unique_hex())
['56', '48', '18', '39', '96', '60', 'b6', '82', '9c', '00', '79', '59', 'fb', 'e0', '81', '1f', 'df', 'f8', 'c7', '43', 'ee', '44', 'cc', '77', 'b7', '92', '75', 'd6', '1a', 'b5', 'f0', '6d', 'a0', 'f9', 'e6', 'e3', '6e', '99', '91', '41', '3c', 'dd', '87', 'c5', '8c', 'e4', '42', '34', '76', '47', '28', '58', '9a', '21', '0e', 'a4', 'b4', '51', '67', 'f2', '5b', '1d', '2d', '04', '2c', '19', 'b3', 'a9', 'f1', '7c', 'eb', '89', '70', 'fc', '14', 'bc', '02', '20', '4b', 'dc', 'c0', 'bf', 'e7', '62', '15', '49', 'a7', '13', 'b2', 'e2', 'f5', '66', '5c', 'd5', 'c1', 'ba', 'c9', '27', '4c', '7e', '24', 'd1', '2e', 'c8', '5d', '55', '83', '54', 'b1', 'ce', '29', '63', '11', '85', 'ff', '5a', 'a3', 'c6', '0b', '0a', 'fd', '65', '8d', 'ca', 'cd', '94', '74', '16', '71', '26', 'ad', '3f', 'ac', 'bb', 'd4', '08', 'b8', 'fa', '72', '31', '23', '37', '86', 'ed', '09', '1e', 'f6', 'cf', '50', '4e', '3e', 'a1', '80', '52', 'af', '03', 'f7', 'da', '95', '4f', '38', 'd8', 'b0', '17', '2b', '8e', '3a', 'fe', '78', 'c2', '45', 'ef', '32', 'de', 'c4', 'd7', '2f', '05', '36', '07', '46', '22', '6c', 'd0', '4d', '64', 'a2', '53', 'e1', '6b', '6f', 'c3', '12', '9b', 'd3', '25', '0f', 'e9', 'be', '35', 'a5', '30', '7f', '8f', '9e', '06', '0d', '8a', '5f', '68', '73', '57', '01', '2a', '4a', '88', 'f3', 'ae', 'aa', '9d', '97', '93', 'ec', 'd2', '7a', 'd9', 'db', '90', '84', '3d', '1b', 'f4', '6a', 'b9', '7b', '1c', 'a6', 'a8', '69', 'cb', '98', '7d', 'ab', '40', 'e8', '3b', 'ea', '10', 'e5', 'bd', '61', '33', '5e', '9f', '8b', '0c']

然后你的正则表达式成为:

re.sub(r'\{\}', lambda m: next(unique_hex()), random_string)

0
投票

正如我在OP评论中暗示的那样,如果只有{}s,其间没有字符,则以下工作(使用Python 3.6测试)

import binascii
import os

random_string = """

asdasdasd23123asdnos{}n
asdnoiaf{}aosndiasdasd
asd{}1239i123nig
hpodpfh{}askmdiasd
{}asdasdasd
"""


def get_random(n):
    out = set()
    while len(out) < n:
        out.add(binascii.b2a_hex(os.urandom(1)).decode())
    return list(out)


print(random_string.format(*get_random(random_string.count('{}'))))

输出:

asdasdasd23123asdnos51n
asdnoiaf50aosndiasdasd
asdf91239i123nig
hpodpfhb6askmdiasd
87asdasdasd
© www.soinside.com 2019 - 2024. All rights reserved.