python dateutil unicode warning
Asked Answered
E

2

6

im parsing some tweet's data from Twitter API using sixohsix library. Im trying to convert the date of the tweet to my locale:

from pytz import timezone
from dateutil import parser

timestamp = parser.parse(tweet["created_at"])
timestamp_arg = timestamp.astimezone(timezone('America/Buenos_Aires'))

and im getting a unicode warning:

dateutil\parser.py:339: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal elif res.tzname and res.tzname in time.tzname:

I've tried doing

parser.parse(str(tweet["created_at"]))
parser.parse(unicode(tweet["created_at"]).encode())

But nothing changes.

Besides the warning nothing seems to be broken. Does anyone know why is this happening, and how to fix it?

Thanks!

UPDATE:

I've tried the same example but hardcoding the time to string and that works without the warning. Also according to the warning msg the issue seems to happen in the parse call, in parser.py:339 when doing

res.tzname in time.tzname

maybe because res is unicode and time.tzname is not??

Edaedacious answered 22/1, 2014 at 23:41 Comment(5)
Can you post the entire stack trace? Which line is failing?Meteorology
Also, the error you're getting may suggest the use of u'America/Buenos_Aires'Meteorology
That encode() at the end is unlikely to help anything, since the warning has obviously already happened before you get that far. And generally, just calling str or unicode on things without an encoding is a very bad idea, especially things that came off the internet and therefore are likely to have a different encoding than your system's default.Prewar
Also, if you're asking questions about a third-party library like dateutil, it's usually worth mentioning the version. Especially in the case of a library that's relatively recently been ported to dual-2.x/3.x-compatible code, and could easily have had a bug in the version you have that was fixed the next day.Prewar
so @goncalopp not sure how to do that, since there's no exception or "crash" just the warning and script keeps going. dateutil version is 2.2 (installed today).Edaedacious
T
5

This is an unsolved bug in dateutil (as of version 2.2) that only occur on Windows: https://bugs.launchpad.net/dateutil/+bug/1227221

Dateutil will still behave correctly unless you try to parse timezones with non-ascii-characters. I assume this is very unusual, so you should be fine.

The easiest workaround is probably just to silence the errors.

import warnings
warnings.filterwarnings("ignore", category=UnicodeWarning)
Tirpitz answered 3/5, 2014 at 22:38 Comment(0)
M
3

Only 18 month after the question and 15 month after the answer, we have probably solved this bug: https://github.com/dateutil/dateutil/issues/92 So the current solution is to upgrade to the development version of dateutil or any version >=2.5.0

Money answered 2/8, 2015 at 17:38 Comment(2)
Hello. This bug is still on windows, dateutil version is v2.5.3.Wheaton
v2.6.0 still throw UnicodeWarning .... elif res.tzname and res.tzname in time.tznameToddle

© 2022 - 2024 — McMap. All rights reserved.