python-dateutil

How to parse multiple dates from a block of text in Python (or another language)

久未见 提交于 2019-11-30 11:38:38
I have a string that has several date values in it, and I want to parse them all out. The string is natural language, so the best thing I've found so far is dateutil . Unfortunately, if a string has multiple date values in it, dateutil throws an error: >>> s = "I like peas on 2011-04-23, and I also like them on easter and my birthday, the 29th of July, 1928" >>> parse(s, fuzzy=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/pymodules/python2.7/dateutil/parser.py", line 697, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "/usr/lib

Python dateutil.parser throws “ValueError: day is out of range for month”

99封情书 提交于 2019-11-30 03:33:47
问题 I have a the following code that runs fine with input format like {Year}/{Month} except when it comes to 1994/02 Here is the sample code >>> import dateutil.parser as dtp >>> dtp.parse('1994/01') datetime.datetime(1994, 1, 29, 0, 0) >>> dtp.parse('1994/03') datetime.datetime(1994, 3, 29, 0, 0) >>> dtp.parse('1994/02') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/antony/.virtualenvs/comp-invest/lib/python2.7/site-packages/dateutil/parser.py", line 720, in

Trouble in parsing date using dateutil

我们两清 提交于 2019-11-29 13:33:26
I am using python-dateutil for parsing a date from a string: import dateutil.parser print dateutil.parser.parse('some null string', fuzzy=True).date() 2012-10-18 print dateutil.parser.parse('some 31 Oct 2012 string', fuzzy=True).date() 2012-10-31 What I am expecting is for dateutil.parser.parse('some null string', fuzzy=True).date() to throw an exception, but it's returning the current date. Can someone show me how I can avoid getting the current date, if no date is found in the provided string? Thanks in advance. See the dateutil docs , specifically the parse function (emphasizes mine):

Python newbie - PIP / invalid syntax error [duplicate]

こ雲淡風輕ζ 提交于 2019-11-29 11:57:13
问题 This question already has answers here : Why does “pip install” inside Python raise a SyntaxError? (7 answers) Closed 4 years ago . Ok, totally newbie to programming and python. Running Windows 7, python 2.7 x64. I am trying in install dateutil package using pip . I installed pip , numpy and pandas ... which were pretty straightforward as they are exe files. I am now trying to use pip to install dateutil . In the Python Shell, I have typed: pip install dateutil and pip install python-dateutil

Python dateutils print recurrence rule according to iCalendar format (see RFC 5545)

佐手、 提交于 2019-11-29 04:08:31
I am trying to print a recurrence rule as a string specified by iCalendar format (see RFC 5545 ). Im using python dateutils, in particular dateutil.rrule to create the recurrence rule and I want to print this as a string like: "RRULE:FREQ=DAILY;COUNT=5" Can anyone tell me if there is a method to achieve this? I think I'm using the labix dateutils btw. Many thanks! There is no method or function in the python-dateutil package to do this. See this bug for a patch that may help: https://bugs.launchpad.net/dateutil/+bug/943512 . Although this is written four years after the question was asked,

Python dateutil.parser.parse parses month first, not day

拟墨画扇 提交于 2019-11-29 02:58:26
I'm using dateutil.parser.parse to format a date from a string. But now it mixes up the month and the day. I have a string that contains 05.01.2015 . After dateutil.parser.parse("05.01.2015") it returns: datetime.datetime(2015, 5, 1, 0, 0) I hoped the it would return (2015, 1, 5, 0, 0) How can I tell the code that the format is dd.mm.yyyy ? For the record, 25.01.2015 will be parsed as (2015, 1, 25, 0, 0) , as expected. Specify dayfirst=True : >>> dateutil.parser.parse("05.01.2015", dayfirst=True) datetime.datetime(2015, 1, 5, 0, 0) This gives precedence to the DD-MM-YYYY format instead of MM

From a timezone and a UTC time, get the difference in seconds vs local time at that point in time

China☆狼群 提交于 2019-11-28 12:51:50
This should be very simple, but I can't quite figure it out in Python. I want to have a function which takes two arguments, a UTC time in seconds and a zoneinfo name like 'Europe/Vienna' and returns the offset in seconds from local time and UTC for that point in time. In C it would be: /* ... code to to set local time to the time zone I want to compare against, not shown here. Then call function below to get difference vs localtime. Hardly an ideal solution, but just to demonstrate what I want in a "lingua franca" (C): */ int get_diff_vs_localtime(const time_t original_utc_time) { struct tm*

How to install python-dateutil on Windows?

牧云@^-^@ 提交于 2019-11-28 07:08:20
I'm trying to convert some date/times to UTC, which I thought would be dead simple in Python - batteries included, right? Well, it would be simple except that Python (2.6) doesn't include any tzinfo classes. No problem, a quick search turns up python-dateutil which should do exactly what I need. The problem is that I need to install it on Windows. I was able to upack the .tar.gz2 distribution using 7-zip, but now I'm left with a collection of files and no guidance on how to proceed. When I try to run setup.py I get the error "No module named setuptools". Stan Reshetnyk If dateutil is missing

Timezone offset sign reversed by Python dateutil?

喜欢而已 提交于 2019-11-27 23:17:58
Does anyone know why python's dateutil reverses the sign of the GMT offset when it parses the datetime field? Apparently this feature is a known outcome of not only dateutil but also other parsing functions. But this results in an INCORRECT datetime result unless a pre-processing hack is applied: from dateutil import parser jsDT = 'Fri Jan 02 2015 03:04:05.678910 GMT-0800' python_datetime = parser.parse(jsDT) print(python_datetime) >>> 2015-01-02 03:04:05.678910+08:00 jsDT = 'Fri Jan 02 2015 03:04:05.678910 GMT-0800' if '-' in jsDT: jsDT = jsDT.replace('-','+') elif '+' in jsDT: jsDT = jsDT

Python datetime strptime() and strftime(): how to preserve the timezone information

旧街凉风 提交于 2019-11-27 18:28:13
See the following code: import datetime import pytz fmt = '%Y-%m-%d %H:%M:%S %Z' d = datetime.datetime.now(pytz.timezone("America/New_York")) d_string = d.strftime(fmt) d2 = datetime.datetime.strptime(d_string, fmt) print d_string print d2.strftime(fmt) the output is 2013-02-07 17:42:31 EST 2013-02-07 17:42:31 The timezone information simply got lost in the translation. If I switch '%Z' to '%z', I get ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z' I know I can use python-dateutil , but I just found it bizzare that I can't achieve this simple feature in datetime and have to