Converting DD MM YYYY into YYYY-MM-DD?
Rami Chowdhury
rami.chowdhury at gmail.com
Tue Aug 18 04:11:20 EDT 2009
Could you let me know which platform this is on (Windows, *nix)? It may be a
locale encoding issue -- the locale.setlocale() function allows the second
argument to be a tuple of (locale_code, encoding), as below:
locale.setlocale(locale.LC_ALL, ('FR', 'UTF-8'))
Since this is for a one-shot (and presumably threading-agnostic) program, and
a fairly trivially formatted date-string, I would suggest using
datetime.strptime
(http://docs.python.org/library/datetime.html#datetime.datetime.strptime) and
not regular expressions (which IIRC have Issues with non-ASCII characters).
----
Rami Chowdhury
"Ninety percent of everything is crap." -- Sturgeon's Law
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
On Tuesday 18 August 2009 00:49:41 Gilles Ganault wrote:
> On Tue, 18 Aug 2009 17:10:50 +1000, Ben Finney
>
> <ben+python at benfinney.id.au> wrote:
> >Luckily, you have access to the documentation to find out.
>
> I never used groups before. Thanks for showing me.
>
> At this point, the script is almost done, but the regex fails if the
> month contains accented characters (eg. "Août", but fine if eg.
> "Jan").
>
> Adding a line to load the French locale doesn't help :-/
>
> Any idea what I could do to keep the regex happy?
>
> Thank you.
>
> ==============
> import re
> import apsw
> import locale
>
> #In case error due to accent in month name, but no soup 4 U
> locale.setlocale(locale.LC_ALL, 'FR')
>
> connection=apsw.Connection("test.sqlite")
> cursor=connection.cursor()
>
> re_inscription =
> re.compile(r"(?P<date>\d+)\s+(?P<month>\w+)\s+(?P<year>\d+)")
>
> sql = 'SELECT id,dateinscription,dateconnexion FROM mytable'
> rows=list(cursor.execute(sql))
> for row in rows:
> dateinscription = row[1]
> dateconnexion = row[2]
>
> #Prints OK
> print dateinscription
>
> m = re_inscription.search(dateinscription)
> if m:
> day = m.group("date")
> month = m.group("month")
> year = m.group("year")
> print "%s-%s-%s" % (year,month,day)
> else:
> print "No go"
> ==============
More information about the Python-list
mailing list