0
x='''
print '\tone \'piece\' of \"metal\" with \\sharp\\ edge '
print '\nthanks'
'''
exec(x)

I have a string variable (x) and i want to use exec() to print it out correctly. If i have access to the variable i can directly use r' and problem solved, such as :

x=r'''
print '\tone \'piece\' of \"metal\" with \\sharp\\ edge '
print '\nthanks'
'''
exec(x)

But in this case , i don't have access to the variable. so The string variable come from other end or other user. so how can i apply this r' to the existing variable so i can use exec() .

The correct output should be :

    one 'piece' of "metal" with \sharp\ edge 

thanks

The real case is :

Inside my software, i can create objects and each this object can have properties such as text input and button. I will refer this text input and button properties as object.text_input and object.button. Let say i have 2 objects named as AA and BB and i type an expression/script in AA.text_input (the text input of object1). Now from BB (object2) i want to use the expression entered in AA.text_input and execute it using exec(). So in BB.button i will write a code such as : exec(AA.text_input). So the data i grab from AA will be a string. The problem is the code type in text input of AA may contain any character including escape chars and others. So when i use exec() in BB i will have error because of those chars. So the question : how to bring that string from BB.text_input correctly to AA ?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
andio
  • 903
  • 3
  • 17
  • 30
  • 2
    What is the real problem you want to solve? – holdenweb Aug 15 '17 at 11:10
  • Raw string are a help for the interpreeter to arrive at the appropriate bytes from something typeable. If the bytes are already there, not much justification remains. So: Which format comes from the other end and what do you expect instead? – guidot Aug 15 '17 at 11:25
  • I have update my question above , pls have a look .Thanks a lot – andio Aug 15 '17 at 11:53
  • There is no difference between `\n` that you typed and a real newline. – Josh Lee Aug 15 '17 at 12:01

1 Answers1

0

As far as I understand, once 'x' is declared it losing all memory of what characters were typed in, e.g. it replaces '\n' with a newline character.

The only way I can think of reverting this would be to write a statement to replace all the characters in x with the original characters.

For example:

for char in x:
    if char == '\n':
         #replace with '\\n'

Edit: One way to do this, for this example, is

x = x.replace('\\', '\\\\')
x = x.replace('\n', '\\n')
x = x.replace('\t', '\\t')
x = x.replace('\"', '\\\"')
x = x.replace('\'', '\\\'')
Dant
  • 92
  • 5
  • Thanks ,that's what i'm thinking , escape chars like \n or \t etc can be handled but i can't handle the ' or " , for example **x="print 'test \'fail\' ' "** as you said once 'x' is declared , the ' and \' chars is losing the memory, so in my example , the ' and \' will be stored as the same ' . when i try to reload it , now the string become : **print 'test 'fail' '** . i lost the **\** . If i use x.replace, it will replace all the ' . Btw the escape char like \n \t etc are pretty much can be solved using x.encode('string_escape'), but i need to handle the single qout and double quot problem. – andio Aug 15 '17 at 19:22
  • I tested my example above, and it seems to work with replacing the `'` and `"`. Are you putting in the triple-backslash for those ones? They require 3 since they are not recognised characters in isolation like 'n' and 't' are. When I tried it on `x="print 'test \'fail\' ' "` , `print x` outputs `print \'test \'fail\' \' ` – Dant Aug 16 '17 at 08:16
  • i need the output to be like this : **print 'test \'fail\' '** or i want to be able to do exec(x) and get the final outpus like : test 'fail' . So more like i want to save a block of python command in a variable , so then i can execute it using exec() later. – andio Aug 16 '17 at 16:57