Change language

NLP | Using dateutil to parse dates.

| | | |

Installation:
dateutil can be installed using pip or easy_install, i.e. sudo pip install dateutil == 2.0 or sudo easy_install dateutil = = 2.0 . Version 2.0 for Python 3 compatibility is not required. Full documentation can be found at http://labix.org/python-dateutil .

Code: parsing examples

# import library

from dateutil import parser

 

print (parser.parse ( ’Thu Sep 25 10:36:28 2010’ ))

  

print (parser.parse ( ’ Thursday, 25. September 2010 10:36 AM’ ))

 

print (parser.parse ( ’9/25/2010 10: 36: 28’ ))

 

print (parser.parse ( ’9/25 / 2010’ ))

 

print (parser.parse ( ’2010-09-25T10: 36: 28Z’ ))

Output:

 datetime.datetime (2010, 9, 25 , 10, 36, 28) datetime.datetime (2010, 9, 25, 10, 36) datetime.datetime (2010, 9, 25, 10, 36, 28) datetime.datetime (2010, 9, 25, 0, 0 ) datetime.datetime (2010, 9, 25, 10, 36, 28, tzinfo = tzutc ()) 

All you need for this — import the parser module and call the parse () function with the datetime string. The parser can return a reasonable datetime object, but it cannot parse the string, it will raise a ValueError. 
How it works:

  • Analyzer instead of looking for recognizable tokens, guess what these tokens refer to. It does not use regular expressions.
  • The order of these tokens matters as it uses a date format that looks like month / day / year (default order), while others use day / month format / year.
  • The parse () function accepts an optional key argument dayfirst, which is False by default to solve this problem.
  • It can parse dates correctly in the latter format if it has set to True.

parser.parse ( ’16/6 / 2019’ , dayfirst = True )

Output:

 datetime.datetime (2016, 6, 16, 0, 0) 

Another ordering problem can occur with two over the years. but & # 39; 11 -6-19 ′ — it is an ambiguous date format. Since by default dateutil is in month-day-year format, & # 39; 11 -6-19 ′ is parsed to 2019. But if yearfirst = True is passed to parse (), it can be parsed for 2011.

print (parser.parse ( ’11-6-19’ ))

print (parser.parse ( ’10-6-25’ , yearfirst = True ))

Output:

 datetime.datetime (2019, 11, 6, 0, 0) datetime.datetime (2011, 6, 19, 0, 0) 

The dateutil parser can also do fuzzy parsing and allows you to ignore extraneous characters in datetime string.  parse () will raise a ValueError with a default value of False when it encounters unknown tokens. A datetime object can usually be returned if fuzzy = True.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method