String formatting strangeness

Discussion in 'Python' started by dark.ryder@gmail.com, May 13, 2005.

  1. Guest

    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?
     
    , May 13, 2005
    #1
    1. Advertising

  2. Peter Hansen Guest

    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
     
    Peter Hansen, May 13, 2005
    #2
    1. Advertising

  3. Larry Bates Guest

    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?
    >
     
    Larry Bates, May 13, 2005
    #3
  4. 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>
     
    Fredrik Lundh, May 13, 2005
    #4
  5. Harry George Guest

    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
     
    Harry George, May 13, 2005
    #5
  6. Guest

    *hides face* Groan! This is what I get for trying to code first thing
    in the morning. Thanks, all, it works fine now...
     
    , May 13, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Munnki
    Replies:
    2
    Views:
    536
    Munnki
    Dec 18, 2003
  2. Joe
    Replies:
    3
    Views:
    482
  3. Stimp

    CDONTS strangeness

    Stimp, May 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    422
    Tarren
    May 17, 2005
  4. Gennady

    String#scan strangeness

    Gennady, Jun 10, 2004, in forum: Ruby
    Replies:
    6
    Views:
    161
    Gennady
    Jun 11, 2004
  5. Belorion

    String.sub strangeness

    Belorion, Apr 18, 2005, in forum: Ruby
    Replies:
    3
    Views:
    101
    Robert Klemme
    Apr 18, 2005
Loading...

Share This Page