Eval and raw string ??

M

Mark

Eval() doesn't seem to recognize the r'string' format. Is there a way
around this.
Example:
If I input: ---------> eval("r'C:\tklll\ndfd\bll'")
I get the output:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
eval("r'C:\tklll\ndfd\bll'")
File "<string>", line 1
r'C: klll
^
SyntaxError: EOL while scanning single-quoted string

The same principle applies for exec.

Thanks in advance,
Mark
 
F

Frederick Polgardy

Eval() doesn't seem to recognize the r'string' format. Is there a way
around this.
Example:
If I input: ---------> eval("r'C:\tklll\ndfd\bll'")
I get the output:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
eval("r'C:\tklll\ndfd\bll'")
File "<string>", line 1
r'C: klll
^
SyntaxError: EOL while scanning single-quoted string

The same principle applies for exec.

Thanks in advance,
Mark

The r'' format is purely for the interpreter; eval wouldn't know the
difference.

Your problem is that eval and exec evaluate source. You're trying to
do execfile().

Fred
 
P

Peter Otten

Mark said:
Eval() doesn't seem to recognize the r'string' format. Is there a way
around this.
Example:
If I input: ---------> eval("r'C:\tklll\ndfd\bll'")
I get the output:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
eval("r'C:\tklll\ndfd\bll'")
File "<string>", line 1
r'C: klll
^
SyntaxError: EOL while scanning single-quoted string

The string you are passing to eval already contains that newline. Use a raw
string instead:
'C:\\tklll\\ndfd\\bll'

Peter
 
P

Paul McGuire

Eval() doesn't seem to recognize the r'string' format. Is there a way
around this.
Example:
If I input: ---------> eval("r'C:\tklll\ndfd\bll'")
I get the output:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
eval("r'C:\tklll\ndfd\bll'")
File "<string>", line 1
r'C: klll
^
SyntaxError: EOL while scanning single-quoted string

The same principle applies for exec.

Thanks in advance,
Mark

This is not a raw string: "r'\tsomething in quotes'". It is a string
starting with an "r", a "'", a tab, and and "s".

This is a raw string: r'\tsomething in quotes'. It is a string
starting with a "\", a "t" and an "s".

Notice that the \t and \n in c:\tkllll\ndfd\bll were treated like tab
and newline? Try eval(r'c:\tkllll\ndfd\bll')

(You will get a different error now, but it wont be a raw string
problem.)

-- Paul
 
M

Matthew Woodcraft

Mark said:
Eval() doesn't seem to recognize the r'string' format. Is there a way
around this.
Example:
If I input: ---------> eval("r'C:\tklll\ndfd\bll'")
I get the output:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
eval("r'C:\tklll\ndfd\bll'")
File "<string>", line 1
r'C: klll
^
SyntaxError: EOL while scanning single-quoted string

The same principle applies for exec.


The \n is being converted to a newline before the string is passed to eval.

Try eval(r"r'C:\tklll\ndfd\bll'")

-M-
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top