gpt4 book ai didi

python - 在一定范围内生成随机Unicode

转载 作者:太空狗 更新时间:2023-10-30 02:34:34 24 4
gpt4 key购买 nike

我正在尝试使用两个起始数字+字母组合生成随机 Unicode 字符..

我已尝试以下操作,但出现错误。

def rand_unicode():
b = ['03','20']
l = ''.join([random.choice('ABCDEF0123456789') for x in xrange(2)])
return unicode(u'\u'+random.choice(b)+l,'utf8')

我得到的错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: end of string in escape sequence

我使用 Python 2.6。

最佳答案

是的,呃,不是这样的。

return unichr(random.choice((0x300, 0x2000)) + random.randint(0, 0xff))

关于python - 在一定范围内生成随机Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7543945/

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