为了获得唯一的URL名称,我正在使用UUID.
>>> url = "https://www.google.co.in/?gfe_rd=cr&ei=-koUWPf4HqzT8ge2g6HoBg&gws_rd=ssl"
>>> uuidstring = str(uuid.uuid5(uuid.NAMESPACE_DNS, url))
但我想进一步缩短名称.有什么方法可以将字符串缩短为唯一的小字符串.
我尝试了base64,但我不知道.
>>> uuid.UUID(uuidstring).bytes.encode('base64').rstrip('=\n').replace('/', '_')
>>> AttributeError: 'bytes' object has no attribute 'encode'
链接的问题:Convert UUID 32-character hex string into a “YouTube-style” short id and back
import uuid,base64
url = "https://www.google.co.in/?gfe_rd=cr&ei=-koUWPf4HqzT8ge2g6HoBg&gws_rd=ssl"
uuidstring = str(uuid.uuid5(uuid.NAMESPACE_DNS, url))
z=base64.encodebytes(uuid.UUID(uuidstring).bytes).decode("ascii").rstrip('=\n').replace('/', '_')
print(z)
结果:
pvEA9qOdX8COYyJf8zgzRA