473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 7635
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
5
by: Sheila King | last post by:
I have a web app that has been running just fine for several months under Python 2.2.2. We are preparing to upgrade the server to run Python 2.4.1. However, part of my web app is throwing an...
4
by: Andy Leszczynski | last post by:
Python 2.2/Unix >>time.strftime("%T") '22:12:15' >>time.strftime("%X") '22:12:17' Python 2.3/Windows >>time.strftime("%X")
6
by: Marcus Kwok | last post by:
I have a simple LogFile class that I use to log messages, and each message has the current date and time prepended to the message. Currently, I am using strftime() to format the date, and placing...
2
by: Mike Conmackie | last post by:
Greetings, Is there any way to force strftime() to ignore locale settings when formatting the resulting string? I have a requirement to create a specific date-time string format in UTC. ...
1
by: Dennis Benzinger | last post by:
Is there a library with a strftime replacement which supports Unicode format strings? Bye, Dennis
12
by: param | last post by:
Hi All, I want to use %x as directive to get the locale specific date representation. But, at the same time, i want to know the format of the output for interpreting it through program. Is there...
2
by: Giovanni Bajo | last post by:
Hello, I am trying to find a good way to portably get the output of strftime() and put it onto a dialog (I'm using PyQt, but it doesn't really matter). The problem is that I need to decode the...
0
by: Alexandr N Zamaraev | last post by:
Subj is bag? Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) on win32 Type "help", "copyright", "credits" or "license" for more information. '2008_05_07 12_30_22.csv' Traceback (most recent...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.