python eval 函数中的 \r\n 与 \n
为什么 eval 函数不能与 \r\n 一起使用,而可以与 \n 一起使用。 例如 eval("for i in range(5):\r\n print 'hello'") 不起作用 eval("for i in range(5):\n print 'hello'") 有效
我知道使用替换("\r","") 不会导致问题被纠正,但有人知道为什么会发生?
- 编辑 - 哦! 抱歉,确切地说,我的意思是执行。 出现回车是因为我正在通过 POST 从 HTML 文本区域读取内容(我在 Linux 机器上)。 现在清楚了,谢谢大家。
Why eval function doesn't work with \r\n but with \n. for example
eval("for i in range(5):\r\n print 'hello'") doesn't work
eval("for i in range(5):\n print 'hello'") works
I know there is not a problem cause using replace("\r","") is corrected, but someone knows why happens?
--Edit--
Oh! sorry , exactly, I meant exec. Carriage returns appeared because I'm reading from a HTML textarea via POST (I'm on a Linux box). now It is clearer, thanks to everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你对“工作”有一个奇怪的定义:
我不确定你为什么使用
eval
——我怀疑你的意思是exec
。 在 Python 中,表达式和语句是完全不同的实体 -eval
仅处理表达式(裸表达式也是 也是一个语句,因此exec
可以处理与它以及其他声明)。转向 exec,并思考作为 Python 核心提交者的情况,我认为这是一个轻微的设计错误:就像(多余且无用的)空格、制表符和换页符在接受换行符之前一样被忽略,回车符也应该被忽略(同样多余且无用)。 我道歉:我想我们从来没有考虑过有人可能想要在那里放回车符——但是,那里有例如换页符没有任何意义,而且我们确实接受< em>那个...所以我认为没有理由拒绝回车(或其他 Unicode 非 ANSI 空白,现在在 Python 3 中我们接受标识符中的任意 Unicode 非 ANSI 字母数字)。
如果你关心的话,请在 Python 的问题跟踪器上打开一个问题,并且(除非其他提交者出现不可预见的反对)我想我可以通过 Python 3.2 修复它(应该在 12 到 18 个月内发布——这是一个估计 [知情猜测] ],不是承诺;-)。
You have a strange definition of "work":
I'm not sure why you're using
eval
-- I suspect you meanexec
. Expressions and statements are drastically different entities in Python --eval
only deals with expressions (a bare expression is also a statement, soexec
can deal with it as well as other statements).Turning to
exec
, and pondering the situation as a Python core committer, I think it's a minor mis-design: just like (redundant and useless) spaces, tabs and form feeds just before a NEWLINE are accepted and ignored, so should (just as redundant and useless) carriage returns be. I apologize: I think we never ever considered that somebody might want to put carriage returns there -- but then, it doesn't make any more sense to have e.g. form feeds there, and we do accept that... so I see no rationale for rejecting carriage returns (or other Unicode non-ANSI whitespace, either, now that in Python 3 we accept arbitrary Unicode non-ANSI alphanumerics in identifiers).If you care, please open an issue on Python's issue tracker, and (barring unforeseen opposition by other commiters) I think I can get it fixed by Python 3.2 (which should be out in 12 to 18 months -- that's an estimate [informed guess], not a promise;-).
你的意思是eval还是exec? 请准确发布您运行的内容,以及完整的回溯和错误消息。
问题可能是因为 Python 语法规定行以换行符 ('\n') 终止,而不是两个字符序列 '\r\n'。
一般来说,如果某处有有意义的 '\r',使用 Replace('\r\n', '\n') 会更安全。 如果你一开始就没有 '\r' 那就更好了...你如何获取文本——在 Windows 机器上读取二进制?
谈到安全性,在从可能的敌人那里获得的任何旧代码上使用 eval 或 exec 时应该小心。
Do you mean eval or exec? Please post exactly what you ran, plus the full traceback and error message.
The problem is probably because the Python grammar says that lines are terminated by newlines ('\n'), not the two-character sequence '\r\n'.
In general, it would be safer for you to use replace('\r\n', '\n') in case there's a meaningful '\r' in there somewhere. It would be better if you didn't have the '\r' there in the first place ... how are you obtaining the text -- binary read on a Windows box??
Talking about safety, you should be careful about using eval or exec on any old code obtained from a possible enemy.