Thursday, 15 May 2014

python - convert date from numpyarray into datetime type -> getting mystic error -



python - convert date from numpyarray into datetime type -> getting mystic error -

i load file f numpy.loadtxt function , wanted extract dates. date has format this: 15.08. - 21.08.2011

numpyarr = loadtxt(f, dtype=str, delimiter=";", skiprows = 1) alldate = numpyarr[:,0][0] dat = datetime.datetime.strptime(alldate,"%d.%m. - %d.%m.%y")

and here whole error:

traceback (most recent phone call last): file "c:\python\test datetime_2.py", line 52, in <module> dat = datetime.datetime.strptime(alldate,"%d.%m. - %d.%m.%y") file "c:\python27\lib\_strptime.py", line 308, in _strptime format_regex = _timere_cache.compile(format) file "c:\python27\lib\_strptime.py", line 265, in compile homecoming re_compile(self.pattern(format), ignorecase) file "c:\python27\lib\re.py", line 190, in compile homecoming _compile(pattern, flags) file "c:\python27\lib\re.py", line 242, in _compile raise error, v # invalid look sre_constants.error: redefinition of grouping name 'd' grouping 3; grouping 1

does have thought going on?

a datetime holds single date & time, while field contains 2 dates , trys extract them single variable. specifically, error you're getting because you've used %d , %m twice.

you can seek along lines of:

a = datetime.datetime.strptime(alldate.split('-')[0],"%d.%m. ") b = datetime.datetime.strptime(alldate.split('-')[1]," %d.%m.%y") = datetime.datetime(b.year, a.month, a.day)

(it's not best code, demonstrates fact there 2 dates in 2 different formats hiding in string).

python datetime numpy

No comments:

Post a Comment