i convert unicode characters encoded 'utf-8' hex string of character starts '%' because api server recognize form.
for example, if need input unicode character '※' server, should convert character string '%e2%80%bb'. (it not matter whether character upper or lower.)
i found way convert unicode character bytes , convert bytes hex string in https://stackoverflow.com/a/35599781.
>>> print('※'.encode('utf-8')) b'\xe2\x80\xbb' >>> print('※'.encode('utf-8').hex()) e280bb
but need form of starting '%' '%e2%80%bb' or %e2%80%bb'
are there concise way implement this? or need make function add '%' each hex character?
there 2 ways this:
the preferred solution. use urllib.parse.urlencode
, specify multiple parameters , encode @ once:
urllib.parse.urlencode({'parameter': '※', 'test': 'true'}) # parameter=%e2%80%bb&test=true
or, can manually convert chunks of 2 symbols, join %
symbol:
def encode_symbol(symbol): symbol_hex = symbol.encode('utf-8').hex() symbol_groups = [symbol_hex[i:i + 2].upper() in range(0, len(symbol_hex), 2)] symbol_groups.insert(0, '') return '%'.join(symbol_groups) encode_symbol('※') # %e2%80%bb
Comments
Post a Comment