所以,例如,我有一个列表:myList=["asdf","ghjk","qwer","tyui"]
我还有一个我要删除的项目的索引号列表:( removeIndexList=[1,3]我想从上面的列表中删除项目1和3)
最好的方法是什么?
我需要做这样的帖子,但是我需要创建一个可以给出输入并多次输出的子进程.该帖子的接受答案有良好的代码......
from subprocess import Popen, PIPE, STDOUT
p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
grep_stdout = p.communicate(input=b'one\ntwo\nthree\nfour\nfive\nsix\n')[0]
print(grep_stdout.decode())
# four
# five
Run Code Online (Sandbox Code Playgroud)
......我想继续这样:
grep_stdout2 = p.communicate(input=b'spam\neggs\nfrench fries\nbacon\nspam\nspam\n')[0]
print(grep_stdout2.decode())
# french fries
Run Code Online (Sandbox Code Playgroud)
但是,我得到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py", line 928, in communicate
raise ValueError("Cannot send input after starting communication")
ValueError: Cannot send input after starting communication
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,proc.stdin.write()方法不能让你收集输出.什么是保持线路开放以进行持续输入/输出的最简单方法?
编辑:====================
它看起来像是pexpect一个有用的库,我正在尝试做什么,但我无法让它工作.这是对我实际任务的更完整的解释.我hfst用来获得个别(俄语)单词的语法分析.以下演示了它在bash shell中的行为:
$ hfst-lookup analyser-gt-desc.hfstol
> ?????
????? ?????+N+Neu+Inan+Sg+Acc …Run Code Online (Sandbox Code Playgroud) --editable我正在尝试使用 模式安装本地包pip。当我发出命令时...
$ python3.7 -m pip install --user -e /home/me/my_pkg/
Run Code Online (Sandbox Code Playgroud)
...它正确安装了所有依赖项,但是当它尝试my_pkg自行安装时,我得到以下信息...
Running setup.py develop for udar
Running command /usr/bin/python3.7 -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/me/my_pkg/setup.py'"'"'; __file__='"'"'/home/me/my_pkg/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' develop --no-deps --user --prefix=
running develop
WARNING: The user site-packages directory is disabled.
error: can't create or remove files in install directory
The following error occurred while trying …Run Code Online (Sandbox Code Playgroud) 每次我运行python或python3使用交互式控制台时,提示的显示几乎在第一次或第二次交互后几乎立即失去同步:
>>> [1,2,3]
>>> [1, 2, 3]
print('hi')
>>> hi
Run Code Online (Sandbox Code Playgroud)
然后,当我退出python时,这种行为会延续到bash,另外当你输入时,屏幕上什么都没有出现,但它仍会运行.在以下示例中,我键入date然后history | tail -n3:
$ Thu Oct 8 07:55:47 CEST 2015
$ 488 python
489 date
490 history | tail -n3
Run Code Online (Sandbox Code Playgroud)
但是,打字后reset,一切都恢复正常.
不用说,这种行为非常烦人.原因是什么,我该如何解决?
我在OS X上.使用iTerm或内置的Terminal.app都会产生相同的行为.我的python构建来自macports.这个问题不存在/usr/bin/python(OS X中包含的python 2.7.2).
============================
编辑:如果我使用zsh,那么python仍然有问题,但在zsh退出python之后一切正常.
$TERM是xterm-256color在这两个bash和zsh.
[...]每当我运行我的代码时,我都会得到它,而不是包含一些对象的列表.我想知道它的含义,以便调试我的代码.
我想编写一个视图,要求输入一些输入,然后将其以大写形式返回。我按照将数据从文本框发送到 Flask 中的说明进行操作?.
该应用程序通过 cPanel 使用 Phusion Passenger 进行托管example.com/form。当我点击时Submit,我被送到example.com. example.com/form在我被重定向之前,文本没有任何变化。
我究竟做错了什么?为什么我被重定向?
templates/form.html
<form action="." method="POST">
<textarea name="text"></textarea>
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
form.py
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def form():
return render_template('form.html')
@app.route('/', methods=['POST'])
def form_post():
return request.form['text'].upper()
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用encodepython字符串的方法返回字符的unicode转义码,如下所示:
>>> print( '\xd1\x84'.encode('unicode_escape').decode('utf8') )\n\\u0444\nRun Code Online (Sandbox Code Playgroud)\n\n这适用于非 ASCII 字符,但对于 ASCII 字符,它只返回 ASCII 字符本身:
\n\n>>> print( 'f'.encode('unicode_escape').decode('utf8') )\nf\nRun Code Online (Sandbox Code Playgroud)\n\n期望的输出是\\u0066. 该脚本用于教学目的。
如何获取所有字符的 unicode 十六进制代码?
\n使用带有 NoneType 对象的普通 f 字符串有效:
>>> a = None
>>> f'{a}'
'None'
Run Code Online (Sandbox Code Playgroud)
但是,当使用格式说明符时,它会中断---与 str.format() 一样:
>>> f'{a:>6}'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to NoneType.__format__
>>> '{:>6}'.format(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to NoneType.__format__
Run Code Online (Sandbox Code Playgroud)
出乎意料的是,(至少对我而言)旧的 C 样式字符串格式有效:
>>> '%10s' % a
' None'
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?我不明白为什么f'{a:>6}'不评估为' None'. 为什么格式说明符要打破它?
这是python中的错误吗?如果它是一个错误,我将如何修复它?