Generate random UTF-8 string in Python

后端 未结 8 1394
清酒与你
清酒与你 2020-12-09 08:38

I\'d like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module?

相关标签:
8条回答
  • 2020-12-09 09:20

    Follows a code that print any printable character of UTF-8:

    print(''.join(tuple(chr(i) for i in range(32, 0x110000) if chr(i).isprintable())))
    

    All printable characters are included above, even those that are not printed by the current font. The clause and not chr(i).isspace() can be added to filter out whitespace characters.

    0 讨论(0)
  • Since Unicode is just a range of - well - codes, what about using unichr() to get the unicode string corresponding to a random number between 0 and 0xFFFF?
    (Of course that would give just one codepoint, so iterate as required)

    0 讨论(0)
提交回复
热议问题