Thread: Valueerror

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0

    Valueerror


    I have the following code
    Code:
    <p/>
     Thank you for your Submission. %(name)s,
      Your information has been processed.
    </body>
    </html>
    
    """
    
    print confirm % name
    where confirm is the html code above.

    however when i submit the info to the form and click send, cgitb gives me a value error.
    61 print confirm % name
    confirm = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN...information has been processed.\n</body>\n</html>\n\n', name = 'greg'

    ValueError: unsupported format character '>' (0x3e) at index 346
    args = ("unsupported format character '>' (0x3e) at index 346",)
    however name was taken care of already with the line
    Code:
    name = form["name"].value
    What exactly am i doing wrong please?
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The '%' string formatting operator has two forms, and you are getting the two mixed up.

    The first form takes postional parameters. It has a list or tuple after the %, and fills in the string in order. For this form the string needs to contain unnamed conversion specifiers, e.g. "%s".

    The second form uses named parameters and requires a dictionary object on the right. It looks the name up in the dictionary.

    The code you posted uses the name format on the left and the positional format on the right.

    Any of the following should work:

    Code:
    #use positional format
    "...Thank you for your Submission. %s..." % name
    
    #create a dictionary with one entry
    "...Thank you for your Submission. %(name)s..." % { 'name' : name }
    
    #use the locals() function to get a dictionary of the local namespace
    "...Thank you for your Submission. %(name)s..." % locals()
    Regards,

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    2
    Rep Power
    0

    Similar string substitution problem


    I'm having similar difficulty with this type of error. The error message that I'm getting is:

    Code:
    ValueError: unsupported format character '"' (0x22) at index 39
    Here are the parts of the code that are generating this error:

    1.
    Code:
    write_line = '<td rowspan="2" valign="top" width="90%"><font size="-2" %s>Message:<br>
    <textarea name="message" rows="4" cols="35">%s</textarea>
    </font></td>'
    2.
    Code:
    write_line % ('color="black"', 'TEST 1.. 2.. 3..')
    This has been frustrating me for some time now and I haven't been able to find the solution. I'm hoping that someone here can give me a hand.

    Thanks,
    Ryan
  6. #4
  7. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    just a note - for better back-compatability, "<p/>" should be "<p />" (with the space)
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you want to get a % character in a string that uses string formatting then you need to use the %% formatting character i.e.

    Code:
    >>> print '<td rowspan="2" valign="top" width="90%">%s</td>' % 'TD Content'
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: unsupported format character '"' (0x22) at index 39
    >>> print '<td rowspan="2" valign="top" width="90%%">%s</td>' % 'TD Content'
    <td rowspan="2" valign="top" width="90%">TD Content</td>
    >>>
    Spot the difference . When the value(s) are inserted into the string, the %% formatting characters are simply replaced by a single % character.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    2
    Rep Power
    0
    Thanks for your help. I hadn't thought that that would be the problem, but it makes sense.

IMN logo majestic logo threadwatch logo seochat tools logo