By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,672 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,672 IT Pros & Developers. It's quick & easy.

Eval and raw string ??

P: n/a
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

Aug 22 '07 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Aug 22, 11:06 am, Mark <cree...@gmail.comwrote:
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

Aug 22 '07 #2

P: n/a
Mark wrote:
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:
>>eval(r"r'C:\tklll\ndfd\bll'")
'C:\\tklll\\ndfd\\bll'

Peter
Aug 22 '07 #3

P: n/a
On Aug 22, 11:06 am, Mark <cree...@gmail.comwrote:
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
Aug 22 '07 #4

P: n/a
Mark <cr*****@gmail.comwrote:
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-
Aug 22 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.