似乎他们在Python 3中取消了通过删除快速加载脚本的所有简单方法 execfile()
有没有一个明显的选择我错过了?
我想将已经缩进的Python代码/整个函数和类复制到IPython中.每次我尝试缩进都搞砸了,我得到以下错误信息:
IndentationError: unindent does not match any outer indentation level (<ipython-input-23-354f8c8be51b>, line 12)
If you want to paste code into IPython, try the %paste and %cpaste magic functions.
这段代码,test.py:
if 1:
print "foo"
print "bar"
Run Code Online (Sandbox Code Playgroud)
可以使用execfile("test.py")
或成功执行python test.py
,但是当一个人尝试将其复制粘贴到python解释器中时:
File "<stdin>", line 3
print "bar"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
为什么会这样?可以通过配置解释器以便成功读取复制粘贴的文本吗?我想这可能会影响口译员的输入,但这对我来说没问题.
如何在 python REPL 中编写多行代码?:
aircraftdeMacBook-Pro:~ ldl$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Run Code Online (Sandbox Code Playgroud)
例如示例:
i = 0
while i < 10:
i += 1
print i
Run Code Online (Sandbox Code Playgroud)
在终端中,我不知道在 python shell 中热换行:
我测试了Control+ Enter、Shift+Enter和Command+ Enter,它们都错了:
>>> while i < 10:
... print i
File "<stdin>", line 2
print i
^
IndentationError: expected …
Run Code Online (Sandbox Code Playgroud) 我想知道为什么在交互式提示之间以及从shell作为可执行文件运行程序时,Python中的空白行有不同的规则.
由于空白行被忽略,我喜欢大量使用它们.但是,在交互式提示中,空行用于终止循环.因此,当我将一大块代码粘贴到交互式提示中时,我一直遇到缩进错误,因为在整个循环中我会有空行.因此,这使得交互式调试/开发过程有些繁琐.使用#而不是空行有帮助,但我喜欢我的空白行.
更令人讨厌的是提示之间的不同行为(例如python和ipython).在python交互式提示符会给我一个错误,我希望它,ipython将继续执行缩进代码,就好像它不是抱怨的循环的一部分.
我觉得有一个简单的解决方案,但我不知道.我正在使用vi进行编辑和python/ipython提示.谢谢.
是否有可能在交互式python中测试方法并在其中保留空行?
def f1():
import random
import time
time.sleep(random.randint(1, 4))
Run Code Online (Sandbox Code Playgroud)
这给出了熟悉的错误
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)
所以,是的解决方法是删除函数内的所有空行.我想知道是否真的必须能够以交互模式/ REPL运行.
谢谢
我有点卡住了ConfigParser
.
我想为现有部分添加特定设置.
我做:
import ConfigParser
Config = ConfigParser.ConfigParser()
Config
Config.read("/etc/yum.repos.d/epel.repo")
Config.sections()
Config.set('epel','priority',10)
with open('/etc/yum.repos.d/epel.repo', 'w') as fout:
Run Code Online (Sandbox Code Playgroud)
然后它显示:
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>>
Run Code Online (Sandbox Code Playgroud)
编辑#1
现在我尝试使用iniparse模块.我做了:
from iniparse import INIConfig
cfg = INIConfig(open('/etc/yum.repos.d/epel.repo'))
cfg.epel.priority=10
f = open('/etc/yum.repos.d/epel.repo', 'w')
print >>f, cfg
f.close()
Run Code Online (Sandbox Code Playgroud)
不幸的是,它删除了旧内容.我怎么解决这个问题?
编辑#2
看起来它现在有效.
f = open('/etc/yum.repos.d/epel.repo', 'wb')
Run Code Online (Sandbox Code Playgroud)
做了伎俩.
我似乎无法看到这段代码有什么问题,但是当我运行脚本时,我得到一个IndentationError.
import web
import json
urls = (
'/(.*)', 'handleRequest'
)
app = web.application(urls, globals())
class handleRequest:
def GET(self, method_id):
if not method_id:
return json.dumps({'ok':0})
else:
return json.dumps({'ok': method_id})
def POST(self):
i = web.input()
pass
if __name__ == "__main__":
app.run()
Run Code Online (Sandbox Code Playgroud)
这是我尝试运行脚本时收到的控制台错误消息:
>>> def POST(self):
File "<stdin>", line 1
def POST(self):
^
IndentationError: unexpected indent
>>> i = web.input()
File "<stdin>", line 1
i = web.input()
^
Run Code Online (Sandbox Code Playgroud)
我检查了缩进 - 和我在整个文件中使用的相同的4个空格 - 我错过了什么?!
我在Python 3.3中,我只输入这3行:
import sklearn as sk
import numpy as np
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
SyntaxError: multiple statements found while compiling a single statement
Run Code Online (Sandbox Code Playgroud)
我能做错什么?
编辑:如果有人遇到这个问题,我发现的解决方案是下载Idlex并使用其IDLE版本,它允许多行.
python ×9
copy-paste ×1
indentation ×1
interactive ×1
ipython ×1
python-3.3 ×1
python-3.x ×1
syntax-error ×1
terminal ×1