0

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?

4 Answers 4

4

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'
>>> 
Sign up to request clarification or add additional context in comments.

4 Comments

Not a generic solution at all.
OP didn't ask for the generic answer. Simply asking for the specific case. Iterating dictionary is not a big deal, OP can do it if needed.
@AppCreator, updated answer with oneline solution. Please check it out.
How is this not "generic"? It solves the stated problem and makes proper use of the O(1) lookup in dictionaries.
1

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.

Comments

0

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)

2 Comments

I tried. The problem is that the key is sometimes "apr." , and sometimes "juli" with respective without a dot, hence the format is sometimes '%d %b. %Y' and sometimes '%d %b %Y'.
@AppCreator Made a generic solution for your norwegian dates that works for all the different date types.. maybe useful for you if you dont want to make a custom dictionary
0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.