AppCreator
AppCreator

Reputation: 241

Change substring in a string to an corresponding item in a dictionary using Python

I get a string like "29 jan. 2021". I want to convert the swedish substring "jan." to the corresponding english month name, in this case "Jan" so the result would be "29 Jan 2021". Then I can use this string to convert it into a date object.

I have a dictionary that has a key:value with the swedish key name and corresponding english month name as value:

monthNameDictionary = {
    "jan.": "Jan",
    "feb.": "Feb",
    "mars": "Mar",
    "apr.": "Apr",
    "maj": "May",
    "juni": "Jun",
    "juli": "Jul",
    "aug.": "Aug",
    "sep.": "Sep",
    "okt.": "Oct",
    "nov.": "Nov",
    "dec.": "Dec",
}

How can I use this or some other way to convert the string into a string with the english month name?

Upvotes: 0

Views: 70

Answers (4)

Ajax1234
Ajax1234

Reputation: 71451

You can use re.sub:

import re
monthNameDictionary = {'jan.': 'Jan', 'feb.': 'Feb', 'mars': 'Mar', 'apr.': 'Apr', 'maj': 'May', 'juni': 'Jun', 'juli': 'Jul', 'aug.': 'Aug', 'sep.': 'Sep', 'okt.': 'Oct', 'nov.': 'Nov', 'dec.': 'Dec'}
s = "29 jan. 2021"
new_s = re.sub('(?<=\d\s)[a-z]+\.', lambda x:monthNameDictionary[x.group()], s)

Output:

'29 Jan 2021'

Upvotes: 0

oskros
oskros

Reputation: 3285

You can just use strptime for this specific format, like so:

import datetime as dt
date_obj = dt.datetime.strptime('29 jan. 2021', '%d %b. %Y')

I would generally recommend checking out this page for figuring out how to parse date strings: https://strftime.org/

Finally, if you want to convert it back to another format, you can use strftime

print(date_obj.strftime('%d %b %Y'))

EDIT: I saw you have norwegian? dates in your question, and need to convert those to english. Plus the format changes for some months

Here is a generic solution to your problem

import datetime as dt
import locale

locale.setlocale(locale.LC_ALL, "nn_NO")
dates = ['29 apr. 2021', '20 mars 2021', '1 okt. 2019', '5 juni 1990']
date_objs = []
for d in dates:
    fmt = '%d %b. %Y' if '.' in d else '%d %B %Y'
    date_objs.append(dt.datetime.strptime(d, fmt))

print(date_objs)

Upvotes: 0

surya
surya

Reputation: 809

This should work,

>>> monthNameDictionary = {
    "jan.": "Jan",
    "feb.": "Feb",
    "mars": "Mar",
    "apr.": "Apr",
    "maj": "May",
    "juni": "Jun",
    "juli": "Jul",
    "aug.": "Aug",
    "sep.": "Sep",
    "okt.": "Oct",
    "nov.": "Nov",
    "dec.": "Dec",
}
>>> s = "29 jan. 2021"
>>> op = s.split(" ")
>>> op
['29', 'jan.', '2021']
>>> op[1] = monthNameDictionary[op[1]]
>>> op
['29', 'Jan', '2021']
>>> " ".join(op)
'29 Jan 2021'
>>> 

If you prefer oneline solution, here it is

>>> " ".join([monthNameDictionary.get(i, i) for i in s.split(" ")])
'29 Jan 2021'
>>> 

Upvotes: 4

TheEagle
TheEagle

Reputation: 5992

Just for completeness: If you really want to replace the strings, use this (assumes that every string to be replaced is present at most once):

monthDict = {
    "jan.": "Jan",
    "feb.": "Feb",
    "mars": "Mar",
    "apr.": "Apr",
    "maj": "May",
    "juni": "Jun",
    "juli": "Jul",
    "aug.": "Aug",
    "sep.": "Sep",
    "okt.": "Oct",
    "nov.": "Nov",
    "dec.": "Dec",
}
inStr = "29 jan. 2021"
for month in monthDict: # iterating directly over a dict iterates over it's keys
    inStr = inStr.replace(month, monthDict[month])
print(inStr)

EDIT: If you have multiple occurrences of the same word, use inStr = re.sub(month, monthDict, inStr) instead.

Upvotes: 1

Related Questions