469,916 Members | 2,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

Re: strftime() argument 1 must be str, not unicode

2008/5/7 Alexandr N Zamaraev <to***@promsoft.ru>:
Subj is bag?

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>from datetime import datetime
>>datetime.today().strftime('%Y_%m_%d %H_%M_%S.csv')
'2008_05_07 12_30_22.csv'
>>datetime.today().strftime(u'%Y_%m_%d %H_%M_%S.csv')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: strftime() argument 1 must be str, not unicode
Unicode and str objects are not the same. Why do you think that this
is a bug? Anyway, you can always use 'encode' method of unicode
objects:

In [2]: datetime.today().strftime('%Y-%m-%d %H-%M-%S.csv')
Out[2]: '2008-05-07 10-49-24.csv'

In [3]: datetime.today().strftime(u'%Y-%m-%d %H-%M-%S.csv')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/home/mishok/doc/python/<ipython consolein <module>()

TypeError: strftime() argument 1 must be str, not unicode

In [4]: datetime.today().strftime(u'%Y-%m-%d %H-%M-%S.csv'.encode('utf-8'))
Out[4]: '2008-05-07 10-51-19.csv'

No offence, but have you read the tutorial?
--
http://mail.python.org/mailman/listinfo/python-list


--
Wbr, Andrii Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
Jun 27 '08 #1
3 7345
"Andrii V. Mishkovskyi" <mi******@gmail.comwrote:
>2008/5/7 Alexandr N Zamaraev <to***@promsoft.ru>:
>Subj is bag?

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
> >>from datetime import datetime
datetime.today().strftime('%Y_%m_%d %H_%M_%S.csv')
'2008_05_07 12_30_22.csv'
> >>datetime.today().strftime(u'%Y_%m_%d %H_%M_%S.csv')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: strftime() argument 1 must be str, not unicode

Unicode and str objects are not the same. Why do you think that this
is a bug?
I think that's a perfectly reasonable thing to expect. At the risk of
over-generalization, there is no good reason why, by this point in time,
all of the standard library routines that accept strings shouldn't also
accept Unicode strings.

It's the duck typing principle. Unicode strings look, walk, and talk like
regular strings. An error like this is not intuitive.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #2
Tim Roberts wrote:
I think that's a perfectly reasonable thing to expect. At the risk of
over-generalization, there is no good reason why, by this point in time,
all of the standard library routines that accept strings shouldn't also
accept Unicode strings.
However, file(fname), open(fname), os .*, os.path .*,
time.strftime(format) take unicode parameters.
See http://bugs.python.org/issue2782
Jun 27 '08 #3
2008/5/8 Tim Roberts <ti**@probo.com>:
"Andrii V. Mishkovskyi" <mi******@gmail.comwrote:
>2008/5/7 Alexandr N Zamaraev <to***@promsoft.ru>:
Subj is bag?
>>
> Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
>(Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>from datetime import datetime
> >>datetime.today().strftime('%Y_%m_%d %H_%M_%S.csv')
> '2008_05_07 12_30_22.csv'
> >>datetime.today().strftime(u'%Y_%m_%d %H_%M_%S.csv')
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: strftime() argument 1 must be str, not unicode
>
Unicode and str objects are not the same. Why do you think that this
>is a bug?

I think that's a perfectly reasonable thing to expect. At the risk of
over-generalization, there is no good reason why, by this point in time,
all of the standard library routines that accept strings shouldn't also
accept Unicode strings.

It's the duck typing principle. Unicode strings look, walk, and talk like
regular strings. An error like this is not intuitive.
On a second thought -- both of you (you and Alexander) are right. I
changed mind and posted a bug on Roundup already (bug #2782).
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


--
Wbr, Andrii Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Hunter | last post: by
4 posts views Thread by Andy Leszczynski | last post: by
6 posts views Thread by Marcus Kwok | last post: by
1 post views Thread by Dennis Benzinger | last post: by
12 posts views Thread by param | last post: by
2 posts views Thread by Giovanni Bajo | last post: by
reply views Thread by Alexandr N Zamaraev | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.