zoukankan      html  css  js  c++  java
  • python生成随机汉字

    python 随机生成汉字

    第一种方法:Unicode码

    在unicode码中,汉字的范围是(0x4E00, 9FBF)

    这个方法比较简单,但是有个小问题,unicode码中收录了2万多个汉字,包含很多生僻的繁体字.

    第二种方法:GBK2312

    gbk2312对字符的编码采用两个字节相组合,第一个字节的范围是0xB0-0xF7, 第二个字节的范围是0xA1-0xFE.
    对GBK2312编码方式详细的解释请参看GBK2312编码

    GBK2312收录了6千多常用汉字.两种方法的取舍就看需求了.

    复制代码
    import random
    
    def Unicode():
        val = random.randint(0x4e00, 0x9fbf)
        return chr(val)
    
    def GBK2312():
        head = random.randint(0xb0, 0xf7)
        body = random.randint(0xa1, 0xfe)
        val = f'{head:x} {body:x}'
        str = bytes.fromhex(val).decode('gb2312')
        return str
    
    if __name__ == '__main__':
        print(Unicode())
        print(GBK2312())
    复制代码

    第三种方法:列表读取

    复制代码
    # encoding: utf-8
    import random
    
    first_name = ["王", "李", "张", "刘", "赵", "蒋", "孟", "陈", "徐", "杨", "沈", "马", "高", "殷", "上官", "钟", "常"]
    second_name = ["伟", "华", "建国", "洋", "刚", "万里", "爱民", "牧", "陆", "路", "昕", "鑫", "兵", "硕", "志宏", "峰", "磊", "雷", "文","明浩", "光", "超", "军", "达"]
    name = random.choice(first_name) + random.choice(second_name)
    
    print(name)
  • 相关阅读:
    subtitleedit
    NHibernate.Mapping1.1.csgen 模板
    js jqueryhotkeys
    c#.net将对象序列化,反序列化json
    ruby document
    sqlserver2008新数据类型
    [转]杂记
    UVA 532 Dungeon Master
    UVA 10557 XYZZY
    UVA 10129 Play on Words
  • 原文地址:https://www.cnblogs.com/sprouts/p/13215159.html
Copyright © 2011-2022 走看看