Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Format string with single quotes in it

17 views
Skip to first unread message

Bahadir

unread,
Sep 25, 2009, 8:42:11 AM9/25/09
to
Hi there,

My question is simple, but I've been spending some hours over the web
and still struggling to get this right: How do I format a string that
contains single quotes in it?

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)

I get the error:

ValueError: unsupported format character '
' (0xa) at index 5541

I also tried:

# Replace single quotes with \'
str = str.replace("'", "\'")

and doubling the backward slash as well.

What am I doing wrong?

Thank you,

Bahadir

Mark Tolonen

unread,
Sep 25, 2009, 9:23:23 AM9/25/09
to pytho...@python.org

"Bahadir" <bilgeha...@gmail.com> wrote in message
news:65b6ce03-62c7-4e56...@l31g2000vbp.googlegroups.com...

A short, working example that exhibits the problem would be helpful, but I
suspect you have a line in your file that ends in a %. The code below
produces the same error.

s = "Here's a percentage: %d%\n"
print s % (0,0)

OUTPUT:

Traceback (most recent call last):
File
"C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 436, in ImportFile
my_reload(sys.modules[modName])
File "w.py", line 2, in <module>
print s % (0,0)
ValueError: unsupported format character '
' (0xa) at index 24

Inspect your input file. If you want a percent sign in a format string, use
%%.

s = "Here's percentages: %d%% %d%%\n"
print s % (0,0)

OUTPUT:

Here's percentages: 0% 0%

-Mark


Jon Clements

unread,
Sep 25, 2009, 9:36:24 AM9/25/09
to
On 25 Sep, 13:42, Bahadir <bilgehan.bal...@gmail.com> wrote:
> Hi there,
>
> My question is simple, but I've been spending some hours over the web
> and still struggling to get this right: How do I format a string that
> contains single quotes in it?
>
> 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)


Don't call variables 'str' as it'll mask the builtin string type.

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

Index 5541!? I would guess the lines in the file aren't quite what you
think. The message you're receiving suggests you've got a % character,
followed by a linefeed character -- are you opening the file in text
mode, if not, do so... and if your file has mixed newline sequences,
try using the universal newline option: infile = open
('filename','rU').

> I also tried:
>
> # Replace single quotes with \'
> str = str.replace("'", "\'")
>
> and doubling the backward slash as well.

None of that is needed AFAICT.

>
> What am I doing wrong?
>
> Thank you,
>
> Bahadir

In summary:

jon@jon-desktop:~$ cat test.txt
line 1 SOMELINE%d and some value of %d
line 2 SOMELINE%d and some value of %d
line 3 SOMELINE%d and some value of %d
line 4 SOMELINE%d and some value of %d

jon@jon-desktop:~$ cat test.py
for line in open('test.txt'):
print line.rstrip('\n') % (0, 0)

jon@jon-desktop:~$ python test.py
line 1 SOMELINE0 and some value of 0
line 2 SOMELINE0 and some value of 0
line 3 SOMELINE0 and some value of 0
line 4 SOMELINE0 and some value of 0


hth,
Jon.

Gabriel Genellina

unread,
Sep 25, 2009, 11:33:24 PM9/25/09
to pytho...@python.org
En Fri, 25 Sep 2009 09:42:11 -0300, Bahadir <bilgeha...@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

Bahadir

unread,
Sep 26, 2009, 7:30:12 AM9/26/09
to
On Sep 26, 6:33 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

>
> So, look in your file for lines ending in %
>
> --
> Gabriel Genellina


Hello All,

I fixed it with your help. Sometimes you need that extra insight from
comp.lang.python.

Thank you so much,

Bahadir

0 new messages