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
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
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.
> 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
Hello All,
I fixed it with your help. Sometimes you need that extra insight from
comp.lang.python.
Thank you so much,
Bahadir