LoslegenKostenlos loslegen

Datetimes from strings

Often you get dates in different formats. There are many different sources of data that represent dates as strings. Scraping web pages, user input, and text files are just a few. The format strings for mapping datetimes are can be found at strftime. Suppose that you have found dates for the mini-crash of October 1989, given as the string crash_text, and the recession of July 3rd 1990, given as the string recession_text, in different formats. How would you represent both in your Python code?

Diese Übung ist Teil des Kurses

Intermediate Python for Finance

Kurs anzeigen

Anleitung zur Übung

  • Construct a format string that maps to the given text for the mini-crash of 1989.
  • Construct a format string that maps to the text for the recession of 1990.
  • Create a datetime representing the recession of 1990.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

crash_text = "Friday the 13th, Oct, 1989"

# Create a format string mapping the text
crash_format_str = "%____ the %dth, %b, %____"
min_crash = datetime.datetime.strptime(crash_text, crash_format_str)
print(min_crash)

recession_text = "07/03/90"

# Create format string
recession_format_str = "%____/%____/%____"

# Create datetime from text using format string
nineties_rec = datetime.datetime.strptime(____, ____)
print(nineties_rec)
Code bearbeiten und ausführen