在Python中生成随机UTF-8字符串 [英] Generate random UTF-8 string in Python

查看:200
本文介绍了在Python中生成随机UTF-8字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的代码的Unicode处理.有什么我可以放入random.choice()从整个Unicode范围中选择的,最好不是外部模块的东西吗? Google和StackOverflow似乎都没有答案.

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? Neither Google nor StackOverflow seems to have an answer.

看起来这比预期的要复杂,所以我将重述这个问题-以下代码是否足以生成所有有效的

It looks like this is more complex than expected, so I'll rephrase the question - Is the following code sufficient to generate all valid non-control characters in Unicode?

unicode_glyphs = ''.join(
    unichr(char)
    for char in xrange(1114112) # 0x10ffff + 1
    if unicodedata.category(unichr(char))[0] in ('LMNPSZ')
    )

推荐答案

有一个另请参见真的很好,不好的UTF-8示例测试数据.

这篇关于在Python中生成随机UTF-8字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆