Reputation: 13930
I've a normal string, that I like to send to a program, which only eats my string as "\"text\""
, exactly like that. But in Python I can print
it like that but I can't assign it like that. See the following:
My text:
In [12]:
i = fieldList[0]
print str(i.name)
Y03M01D01
Which I can print
as "\"text\""
In [13]:
field_new = '"\\"'+str(i.name)+'\\""'
print field_new
"\"Y03M01D01\""
But this is how it is eaten by the program
In [14]:
field_new
Out[14]:
'"\\"Y03M01D01\\""'
Which is not equal to "\"text\""
and so my code fails.
Any suggestions how to resolve this?
Upvotes: 0
Views: 1254
Reputation: 3325
Using the r
prefix for the string in your comparison will have python treat the string as raw (all backslashes are unescaped).
>>> i = "text"
>>> field_new = '"\\"'+str(i)+'\\""'
>>> field_new
'"\\"text\\""'
>>> field_new == r'"\"text\""'
True
Upvotes: 1