zoukankan      html  css  js  c++  java
  • 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:

    
    
  • 相关阅读:
    MapReduce in MongoDB
    MongoDB的一些基本操作
    谈谈NOSQL
    Java中的反射(1)
    Mybatisの常见面试题
    关于Lombok和自动生成get set方法
    订Pizza(Java)
    美化Div的边框
    爱,死亡和机器人(Love,Death&Robots)
    session与cookie的介绍和两者的区别之其相互的关系
  • 原文地址:https://www.cnblogs.com/sandy-t/p/9683480.html
Copyright © 2011-2022 走看看