之前已经在不同程度上提出了这类问题,但我觉得它没有以简洁的方式回答,所以我再次提出这个问题.
我想用Python运行一个脚本.让我们说是这样的:
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
s = f.read()
print s
Run Code Online (Sandbox Code Playgroud)
获取文件位置,读取它,然后打印其内容.没那么复杂.
好的,那我怎么在C#中运行呢?
这就是我现在拥有的:
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = cmd;
start.Arguments = args;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过code.py位置cmd和filename位置,因为args它不起作用.有人告诉我,我应该通过python.exe的cmd,然后code.py filename作为args …
这可能是在类似的背景下提出的,但是在搜索约20分钟后我无法找到答案,所以我会问.
我编写了一个Python脚本(比方说:scriptA.py)和一个脚本(比如说scriptB.py)
在scriptB中我想用不同的参数多次调用scriptA,每次运行大约需要一个小时,(它是一个巨大的脚本,做了很多东西......不用担心它)我希望能够运行scriptA同时具有所有不同的参数,但我需要等到所有这些都完成后再继续; 我的代码:
import subprocess
#setup
do_setup()
#run scriptA
subprocess.call(scriptA + argumentsA)
subprocess.call(scriptA + argumentsB)
subprocess.call(scriptA + argumentsC)
#finish
do_finish()
Run Code Online (Sandbox Code Playgroud)
我想同时运行所有subprocess.call(),然后等到它们全部完成,我该怎么做?
我试着像这里的例子一样使用线程:
from threading import Thread
import subprocess
def call_script(args)
subprocess.call(args)
#run scriptA
t1 = Thread(target=call_script, args=(scriptA + argumentsA))
t2 = Thread(target=call_script, args=(scriptA + argumentsB))
t3 = Thread(target=call_script, args=(scriptA + argumentsC))
t1.start()
t2.start()
t3.start()
Run Code Online (Sandbox Code Playgroud)
但我不认为这是对的.
在我去之前我怎么知道他们都跑完了do_finish()?
简短的摘要:
我正在尝试创建一个程序,将键盘事件发送到计算机,为了所有目的,模拟事件应被视为键盘上的实际击键.
原帖:
我正在寻找一种使用python生成键盘事件的方法.
假设函数接收到必须模拟按下的键,如下所示:
keyboardevent('a') #lower case 'a'
keyboardevent('B') #upper case 'B'
keyboardevent('->') # right arrow key
def keyboardevent(key):
#code that simulated 'key' being pressed on keyboard
Run Code Online (Sandbox Code Playgroud)
以上显然是示例,但我正在寻找的是库,模块或其他,我可以用它来模拟键盘事件.
注意:这与将字符发送到记事本,或将文本输入字段等不同.我希望python脚本模拟一个实际的键盘事件,计算机会认为确实存在键盘事件.
额外注意:
我不想将键击发送到活动窗口 - 我希望系统能够相信键盘的按键被按下,微妙的区别,因为一些活动窗口不接受某些键组合,或者我想使用键盘快捷键对于通过我的脚本的后台进程,他们不需要通过活动窗口
到目前为止,我已经看过这些事情:
这些都是关于苹果的,并没有任何帮助.
还有这个:
这似乎可能是我需要的,但我找不到它的库或任何文档.
我也搜索了更多的地方,但还没有找到解决方案.
我有一个与此类似的元组列表:
l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]
Run Code Online (Sandbox Code Playgroud)
我想创建一个简单的单行程序,它会给我以下结果:
r = (25, 20) or r = [25, 20] # don't care if tuple or list.
Run Code Online (Sandbox Code Playgroud)
这就像做以下事情:
r = [0, 0]
for t in l:
r[0]+=t[0]
r[1]+=t[1]
Run Code Online (Sandbox Code Playgroud)
我确信这很简单,但我想不出来.
注意:我已经查看了类似的问题:
我一直在使用Python,我设置了以下代码情况:
import timeit
setting = """
import functools
def f(a,b,c):
pass
g = functools.partial(f,c=3)
h = functools.partial(f,b=5,c=3)
i = functools.partial(f,a=4,b=5,c=3)
"""
print timeit.timeit('f(4,5,3)', setup = setting, number=100000)
print timeit.timeit('g(4,5)', setup = setting, number=100000)
print timeit.timeit('h(4)', setup = setting, number=100000)
print timeit.timeit('i()', setup = setting, number=100000)
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
f: 0.181384086609
g: 0.39066195488
h: 0.425783157349
i: 0.391901016235
Run Code Online (Sandbox Code Playgroud)
为什么对部分函数的调用需要更长时间?部分函数是将参数转发到原始函数还是映射静态参数?而且,在Python中是否有一个函数返回一个函数的主体,假定所有参数都是预定义的,就像函数i一样?
我一直在淘天的互联网,我也有类似的一个问题这个.
我需要以原始(纯文本)文本检索控制台输出.但是,如果我能用HTML获得它也很好,我总是可以解析它.唯一的问题是我需要在构建步骤中获取它,这是一个问题,因为它应该可用的位置被截断...
我尝试从以下URL(相对于作业)检索控制台输出:
/consoleText/logText/progressiveText/logText/progressiveHTML这两个文本是纯文本,如果不是截断,将是完美的,HTML也是如此...正是我需要的 - 只是它被截断....
我确信有可能以某种方式检索此信息,因为在查看时/consoleFull会有控制台的实时更新,而不会截断或缓冲.
然而,在检查该网页时,我没有找到我想要的内容,而是找到了它应该存在的代码(我没有包含完整的页面代码,因为它几乎是不相关的,我相信那些回答的人能够找出并知道自己应该有什么)
new Ajax.Request(href,{
method: "post",
parameters: {"start":e.fetchedBytes},
requestHeaders: headers,
onComplete: function(rsp,_) {
var stickToBottom = scroller.isSticking();
var text = rsp.responseText;
if(text!="") {
var p = document.createElement("DIV");
e.appendChild(p); // Needs to be first for IE
// Use "outerHTML" for IE; workaround for:
// http://www.quirksmode.org/bugreports/archives/2004/11/innerhtml_and_t.html
if (p.outerHTML) {
p.outerHTML = '<pre>'+text+'</pre>';
p = e.lastChild;
}
else p.innerHTML = text;
Behaviour.applySubtree(p);
if(stickToBottom) scroller.scrollToBottom();
}
e.fetchedBytes = rsp.getResponseHeader("X-Text-Size"); …Run Code Online (Sandbox Code Playgroud) 我试图使用Python将多个XML文件合并在一起,而不是外部库.XML文件具有嵌套元素.
示例文件1:
<root>
<element1>textA</element1>
<elements>
<nested1>text now</nested1>
</elements>
</root>
Run Code Online (Sandbox Code Playgroud)
示例文件2:
<root>
<element2>textB</element2>
<elements>
<nested1>text after</nested1>
<nested2>new text</nested2>
</elements>
</root>
Run Code Online (Sandbox Code Playgroud)
我想要的是:
<root>
<element1>textA</element1>
<element2>textB</element2>
<elements>
<nested1>text after</nested1>
<nested2>new text</nested2>
</elements>
</root>
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
从这个答案.
from xml.etree import ElementTree as et
def combine_xml(files):
first = None
for filename in files:
data = et.parse(filename).getroot()
if first is None:
first = data
else:
first.extend(data)
if first is not None:
return et.tostring(first)
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
<root>
<element1>textA</element1>
<elements>
<nested1>text now</nested1>
</elements>
<element2>textB</element2>
<elements>
<nested1>text after</nested1>
<nested2>new …Run Code Online (Sandbox Code Playgroud) 编辑我现在意识到API简直不够,甚至没有工作.我想重定向我的问题,我希望能够使用他们的"我感觉很难"来自动神奇地搜索duckduckgo.因此,我可以搜索"stackoverflow",并获取主页(" https://stackoverflow.com/ ")作为我的结果.
我正在使用duckduckgo API.这里
我发现使用时:
r = duckduckgo.query("example")
Run Code Online (Sandbox Code Playgroud)
结果不反映手动搜索,即:
for result in r.results:
print result
Run Code Online (Sandbox Code Playgroud)
结果是:
>>>
>>>
Run Code Online (Sandbox Code Playgroud)
没有.
并在results结果中查找索引错误,因为它是空的.
我怎么能得到搜索结果?
似乎API(根据其记录的例子)应该回答问题并给出一种"我感觉很难吃"的形式 r.answer.text
但是网站是以这样的方式制作的,我无法使用普通方法搜索它并解析结果.
我想知道我应该如何使用此API或本网站的任何其他方法解析搜索结果.
谢谢.
pymssql模块用于支持Windows身份验证.现在看来它没有.虽然在某些地方它仍然表明它应该工作.我一直无法找到这个问题的明确答案,也无法找到解决方案.最相关的链接:
https://groups.google.com/forum/#!topic/pymssql/QDMLTGBNeU0
pymssql 1.0支持它,因为它使用并依赖于MS提供的DLL,它是SQL Server客户端堆栈的一部分.该堆栈负责处理所有NTLM协商等.这意味着它是一个仅限Windows的解决方案.
我可以模拟许多种网络环境,所以我尝试了很多不同的设置.我试图能够使用此脚本使用Windows身份验证连接到远程MSSQL服务器.这就是问题所在.
根据我的研究,包括上面的链接,也有使用Windows身份验证与被该pymssql模块两种方式应该工作.
第一种方法:使用当前用户凭据:
pymssql.connect(server='server')
# credentials come from active windows session
# some research shows that a "trusted=True" keyword should be provided.
Run Code Online (Sandbox Code Playgroud)
第二种方法:使用给定的用户凭据:
pymssql.connect(server='server', user=r'domain\user', password='pass')
# credentials are given in code and somehow converted to a
# windows authentication in the background
# some research shows that a "trusted=True" keyword should be provided.
Run Code Online (Sandbox Code Playgroud)
使用该_mssql模块也是如此.
笔记:
关于该主题的其他问题: …
首先,对这两个问题的明显程度表示道歉; 我对此非常陌生,并且不知道我在做什么.
我正在尝试编写一些内容来将样条插值的Scipy函数应用于值数组.我的代码目前看起来像这样:
import numpy as np
import scipy as sp
from scipy.interpolate import interp1d
x=var
x1 = ([0.1,0.3,0.4])
y1 = [0.2,0.5,0.6]
new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
Run Code Online (Sandbox Code Playgroud)
但当它到达线
new_x = np.linspace(x.min(), x.max(), new_length)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
AttributeError: 'function' object has no attribute 'min'
Run Code Online (Sandbox Code Playgroud)
到目前为止,谷歌搜索等没有发现任何我理解的内容.这是什么意思,我该如何解决?
第二个问题:如何一次输入多行代码?目前,如果我尝试复制整个内容然后将其粘贴到PyLab中,它只输入我的代码的顶行,所以我必须一次将整个内容粘贴到一行中.我怎么绕这个?
python ×9
python-2.7 ×2
.net ×1
api ×1
attributes ×1
c# ×1
elementtree ×1
function ×1
functools ×1
hudson ×1
ironpython ×1
jenkins ×1
list ×1
numpy ×1
parsing ×1
partial ×1
performance ×1
pymssql ×1
python.net ×1
search ×1
xml ×1