My goal is to encode this dict with cyrilic
text:
target_dict = {"Animal": "Cat", "city": "Москва"}
To this (cyrilic lettesrs with lower case
encoded):
Animal=Cat&city=%d0%9c%d0%be%d1%81%d0%ba%d0%b2%d0%b0
By default with python it encodes with UPPER CASE
, this is my code:
import urllib.parse
target_dict = {"Animal": "Cat", "city": "Москва"}
encoded = urllib.parse.urlencode(target_dict)
print(encoded)
It returns city
with UPPER CASE
, i need only city in lower case:
Animal=Cat&city=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0
I need to get city (cyriclic text)
with lower case
, these two results are actually the same, but service that I am trying to connect wants exactly lowercase
.
This code uses same library, but different method:
from urllib import parse
def url_encoded_cyrillic(target_dict):
url_quote = set()
result = parse.urlencode(target_dict)
for value in target_dict.values():
for char in value:
encoded_char = parse.quote(char, safe='')
if encoded_char != char:
url_quote.add(encoded_char)
for encoded_item in url_quote:
result = result.replace(encoded_item, encoded_item.lower())
return result
target_dict = {"Animal": "Cat", "city": "Москва :/- Москва", "car": "", "tOwn": "New York"}
print(url_encoded_cyrillic(target_dict))
it will return:
Animal=Cat&city=%d0%9c%d0%be%d1%81%d0%ba%d0%b2%d0%b0+%3a%2f-+%d0%9c%d0%be%d1%81%d0%ba%d0%b2%d0%b0&car=&tOwn=New+York