I have a string '09 Mai 2022'. I am trying to replace it by a value in the dictionary
Code
import re
dictionary = {"Januar":"January","Februar":"February","März":"March","April":"April","Mai":"May","Juni":"June",
"Juli":"July","August":"August","September":"September","Oktober":"October","November":"November",
"Dezember":"December"}
dict_comb = "|".join(dictionary)
date = '09 Mai.2022'
dates_fo = re.sub(dict_comb, dictionary[k] for k in dict_comb, date)
print(dates_fo)
Expected Output
09 May. 2022
CodePudding user response:
A simple way would be to just loop over keys in your dictionary and replace them in date with the corresponding value:
for k,i in dictionary.items():
date = re.sub(k,i, date)
Output:
print(date)
09 May.2022
CodePudding user response:
Try this:
import re
dictionary = {"Januar":"January","Februar":"February","März":"March","April":"April","Mai":"May","Juni":"June",
"Juli":"July","August":"August","September":"September","Oktober":"October","November":"November",
"Dezember":"December"}
pattern = '|'.join(dictionary.keys())
date = '09 Mai.2022'
dates_fo = re.sub(pattern, lambda m: dictionary.get(m.group(0)), date, flags=re.IGNORECASE)
Output:
>>> print(dates_fo)
09 May.2022
CodePudding user response:
re.sub
2nd argument might be function, it should accept Match object as argument, consider following example:
import re
changes = {'A':'1','B':'2','C':'3'}
def replacement(x):
return changes.get(x.group(0),'?')
text = 'ABCDEF'
print(re.sub(r'.',replacement,text))
output
123???
Here I match any single character (.
) and replace it value from dict
if such key exist in it or ?
otherwise.
CodePudding user response:
You can identify groups for day, month and year and replace the matched expression with a formatted string containing your groups or the corresponding dictionary value:
import re
dictionary = {"Januar":"January","Februar":"February","März":"March","April":"April","Mai":"May","Juni":"June",
"Juli":"July","August":"August","September":"September","Oktober":"October","November":"November",
"Dezember":"December"}
pattern = re.compile(r'(\d{1,2})\s (\w )\W(\d{4}|\d{2})')
date = '09 Mai.2022'
# print(re.findall(pattern, date))
dates_fo = re.sub(pattern,
lambda x: f"{x.group(1)} {dictionary.get(x.group(2), x.group(2))}. {x.group(3)}" ,
date)
print(dates_fo)
Output
09 May. 2022
EDIT: this solution might be more complex than others but in this case you can define a standard format for your date and handle different cases in the regular expression. i.e. your output would be formatted exactly the same if your inputs include whitespaces, dots or other characters you choose to ignore.