• python随机生成字符


    Python2:
    Unicode是一种通用的编码方式,不论是英文字母、汉字、日语还是其他文字都能够对应一个唯一的Unicode编码(序号)。

    chr(100) # 得到整数对应的ascii码(小于256)
    ord('?') # 得到一个ascii字符对应的ascii码
    int('4f60',16) # 从16进制数得到对应的十进制数
    hex(20320) # '0x4f60'
    #随机生成中文:
    import random
    #print unichr(random.randint(0x4E00, 0x9FBF)).encode('utf8')
    def Unicode():
         val = random.randint(0x4E00, 0x9FBF)
         return unichr(val) 
    
    def GB2312():
         head = random.randint(0xB0, 0xCF)
         body = random.randint(0xA, 0xF)
         tail = random.randint(0, 0xF)
         val = ( head << 8 ) | (body << 4) | tail
         str = "%x" % val
         return str.decode('hex').decode('gb2312') 
    
    #获取字符串对应的字节码,Unicode码(16进制,10进制)
    s='图'
    s #'xe5x9bxbe'
    type(s) #<type 'str'>
    s_Unicode=s.decode('utf8')
    s_Unicode_int = ord(s_Unicode)
    “”“
    s_Unicode #u'u56fe'
    s_Unicode_str = repr(s_Unicode)   
    s_Unicode_str #"u'\u56fe'"
    s_Unicode_int = int(s_Unicode_str.replace("u'\u","").replace("'",""),16)
    s_Unicode_int #22270
    print unichr(22270).encode('utf8') #图
    s_Unicode_hex=hex(s_Unicode_int)
    s_Unicode_hex #'0x56fe'
    ”“”
    

    python3:

    
    
  • 相关阅读:
    CopyOnWriteArrayList
    Herriot
    Prefix tree
    hadoop 测试框架
    Hadoop RPC
    OpenCV2马拉松第2圈——读写图片
    LCD深度剖析
    SharePoint 改动passwordWeb Part部署方案
    android小游戏模版—重力感应
    CSS(层叠样式表)基础知识
  • 原文地址:https://www.cnblogs.com/sandy-t/p/9683480.html
Copyright © 2020-2023  润新知