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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically