Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String formatting strangeness

Reply
Thread Tools

String formatting strangeness

 
 
dark.ryder@gmail.com
Guest
Posts: n/a
 
      05-13-2005
I must be doing something wrong, but for the life of me, I can't figure
out what. Here's the code snippet which is giving me grief:

print type(number), type(name), type(seconds // 60), type(seconds % 60)
print "\t\t\t<section number=\"%i\" title=\"%s\" length=\"%i:%i\"/>\n"
% [number, name, seconds // 60, seconds % 60]

(These are lines 49 and 50 of the script; I can post the whole thing if
someone wants, but I think this is enough to see why it's driving me
nuts.)

And the output:

<type 'int'> <type 'str'> <type 'int'> <type 'int'>
Traceback (most recent call last):
File "X:\Music (FLAC)\Post-process new rips.py", line 50, in ?
print "\t\t\t<section number=\"%i\" title=\"%s\"
length=\"%i:%i\"/>\n" % [number, name, seconds // 60, seconds % 60]
TypeError: int argument required

Wait, what? The first line clearly identifies that the the first,
third, and fourth elements are all integers, yet the error says that
*lack* of integers is the problem. If I change all "%i"s to "%d", I
get the same problem, and changing to "%s" (hey, it was worth a shot)
gives "TypeError: not enough arguments for format string" instead.
Huh? I see four placeholders and a four-element tuple.

Can anyone enlighten me here?

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      05-13-2005
wrote:
> I must be doing something wrong, but for the life of me, I can't figure
> out what. Here's the code snippet which is giving me grief:
>
> print type(number), type(name), type(seconds // 60), type(seconds % 60)
> print "\t\t\t<section number=\"%i\" title=\"%s\" length=\"%i:%i\"/>\n"
> % [number, name, seconds // 60, seconds % 60]

[snip]
>
> Wait, what? The first line clearly identifies that the the first,
> third, and fourth elements are all integers, yet the error says that
> *lack* of integers is the problem. If I change all "%i"s to "%d", I
> get the same problem, and changing to "%s" (hey, it was worth a shot)
> gives "TypeError: not enough arguments for format string" instead.
> Huh? I see four placeholders and a four-element tuple.


Nope, you see a four-element list. Try changing it to a tuple...

-Peter
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      05-13-2005
The argument to string format expression needs to be a tuple not a list.

Also, all the string escaping makes this very hard to read. You can
mix single and double quotes to achieve:

print '\t\t\t<section number="%i" title="%s" length="%i:%i"/>\n' % \
(number, name, seconds // 60, seconds % 60)

which IMHO is much easier to read.

Larry Bates

wrote:
> I must be doing something wrong, but for the life of me, I can't figure
> out what. Here's the code snippet which is giving me grief:
>
> print type(number), type(name), type(seconds // 60), type(seconds % 60)
> print "\t\t\t<section number=\"%i\" title=\"%s\" length=\"%i:%i\"/>\n"
> % [number, name, seconds // 60, seconds % 60]
>
> (These are lines 49 and 50 of the script; I can post the whole thing if
> someone wants, but I think this is enough to see why it's driving me
> nuts.)
>
> And the output:
>
> <type 'int'> <type 'str'> <type 'int'> <type 'int'>
> Traceback (most recent call last):
> File "X:\Music (FLAC)\Post-process new rips.py", line 50, in ?
> print "\t\t\t<section number=\"%i\" title=\"%s\"
> length=\"%i:%i\"/>\n" % [number, name, seconds // 60, seconds % 60]
> TypeError: int argument required
>
> Wait, what? The first line clearly identifies that the the first,
> third, and fourth elements are all integers, yet the error says that
> *lack* of integers is the problem. If I change all "%i"s to "%d", I
> get the same problem, and changing to "%s" (hey, it was worth a shot)
> gives "TypeError: not enough arguments for format string" instead.
> Huh? I see four placeholders and a four-element tuple.
>
> Can anyone enlighten me here?
>

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      05-13-2005
wrote:

> <type 'int'> <type 'str'> <type 'int'> <type 'int'>
> Traceback (most recent call last):
> File "X:\Music (FLAC)\Post-process new rips.py", line 50, in ?
> print "\t\t\t<section number=\"%i\" title=\"%s\"
> length=\"%i:%i\"/>\n" % [number, name, seconds // 60, seconds % 60]
> TypeError: int argument required
>
> Wait, what? The first line clearly identifies that the the first,
> third, and fourth elements are all integers, yet the error says that
> *lack* of integers is the problem. If I change all "%i"s to "%d", I
> get the same problem, and changing to "%s" (hey, it was worth a shot)
> gives "TypeError: not enough arguments for format string" instead.
> Huh? I see four placeholders and a four-element tuple.


[number, name, seconds // 60, seconds % 60] is not a tuple.

(number, name, seconds // 60, seconds % 60) is a tuple.

</F>



 
Reply With Quote
 
Harry George
Guest
Posts: n/a
 
      05-13-2005
writes:

> I must be doing something wrong, but for the life of me, I can't figure
> out what. Here's the code snippet which is giving me grief:
>
> print type(number), type(name), type(seconds // 60), type(seconds % 60)
> print "\t\t\t<section number=\"%i\" title=\"%s\" length=\"%i:%i\"/>\n"
> % [number, name, seconds // 60, seconds % 60]
>
> (These are lines 49 and 50 of the script; I can post the whole thing if
> someone wants, but I think this is enough to see why it's driving me
> nuts.)
>
> And the output:
>
> <type 'int'> <type 'str'> <type 'int'> <type 'int'>
> Traceback (most recent call last):
> File "X:\Music (FLAC)\Post-process new rips.py", line 50, in ?
> print "\t\t\t<section number=\"%i\" title=\"%s\"
> length=\"%i:%i\"/>\n" % [number, name, seconds // 60, seconds % 60]
> TypeError: int argument required
>
> Wait, what? The first line clearly identifies that the the first,
> third, and fourth elements are all integers, yet the error says that
> *lack* of integers is the problem. If I change all "%i"s to "%d", I
> get the same problem, and changing to "%s" (hey, it was worth a shot)
> gives "TypeError: not enough arguments for format string" instead.
> Huh? I see four placeholders and a four-element tuple.
>
> Can anyone enlighten me here?
>


I notice you used a list instead of a tuple.
Changing to a tuple gives the desired output:

number=1
name="myname"
seconds=250
print "\t\t\t<section number=\"%i\" title=\"%s\" length=\"%i:%i\"/>\n" \
% (number, name, seconds // 60, seconds % 60)

<section number="1" title="myname" length="4:10"/>

I have no idea why a list has that effect.

PS: When writing XML and HTML, I use single quotes, so I don't have to
escape double quotes:
print '\t\t\t<section number="%i" title="%s" length="%i:%i"/>\n' \
% (number, name, seconds // 60, seconds % 60)


--

6-6M21 BCA CompArch Design Engineering
Phone: (425) 294-4718
 
Reply With Quote
 
dark.ryder@gmail.com
Guest
Posts: n/a
 
      05-13-2005
*hides face* Groan! This is what I get for trying to code first thing
in the morning. Thanks, all, it works fine now...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String.sub strangeness Belorion Ruby 3 04-18-2005 07:34 PM
Bookmarks Strangeness Drude Firefox 0 01-26-2005 01:28 AM
String#scan strangeness Gennady Ruby 6 06-10-2004 11:22 PM
Strangeness in bookmarks are still occurring Keith Bowes Firefox 0 01-06-2004 11:18 PM
Datalist Formatting Strangeness Joe ASP .Net 3 10-18-2003 04:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57