Format string with single quotes in it

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Sep 25 23:33:24 EDT 2009


En Fri, 25 Sep 2009 09:42:11 -0300, Bahadir <bilgehan.balban at gmail.com>  
escribió:

> still struggling to get this right: How do I format a string that
> contains single quotes in it?

Forget single quotes. Your problem doesn't appear to be related to those  
quotes.

> I am reading a file with lines of the form:
>
> CONT%d_VIRTMEM_REGIONS  'Container %d number of virtual regions'
>
> and trying to format this as follows:
>
> str % (0, 0)

It works fine for me:

py> str = "CONT%d_VIRTMEM_REGIONS  'Container %d number of virtual  
regions'"
py> str % (0, 0)
"CONT0_VIRTMEM_REGIONS  'Container 0 number of virtual regions'"

> I get the error:
>
> ValueError: unsupported format character '
> ' (0xa) at index 5541

0xa is \n, the end-of-line marker.

py> str = "test%\n"
py> str % 0
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: unsupported format character '
' (0xa) at index 5

So, look in your file for lines ending in %

-- 
Gabriel Genellina




More information about the Python-list mailing list