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=Москва
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=Москва
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
.
CodePudding user response:
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=Москва :/- Москва&car=&tOwn=New York