0

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?

4
  • How do you pass the string to the program? Commented Nov 20, 2014 at 7:58
  • What's the goal here? Do you want the string to have slashes in it, or are you just using them to escape the double quotes? Commented Nov 20, 2014 at 7:59
  • 1
    i think repr and str are the issues and yet you are not clear what you are asking. Commented Nov 20, 2014 at 8:00
  • @mgilson I would like to have the string to have 2 slashes in it. @Tzach If I do "\"Y03M01D01\"" then it works and when I send field_new it doesn't works Commented Nov 20, 2014 at 8:16

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

If I do this in IPython I get the following: In [4]: field_new = '"\\"'+str(i)+'\\""' In [5]: field_new Out[5]: '"\\"<geoprocessing describe field object object at 0x0CCE8C38>\\""'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.