Format string with single quotes in it

B

Bahadir

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
 
M

Mark Tolonen

Bahadir said:
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?

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
 
J

Jon Clements

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.
 
G

Gabriel Genellina

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 %
 
B

Bahadir

So, look in your file for lines ending in %


Hello All,

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

Thank you so much,

Bahadir
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top