以下代码完美无瑕地工作:
public partial class MainForm : Form
{
string pyInterp = File.ReadAllText(Directory.GetCurrentDirectory() + @"\config\pathToPythonInterpreter.txt");
string pyWeather = @"C:\getWeather.py";
public MainForm()
{
InitializeComponent();
UpdateWeather();
}
public void UpdateWeather()
{
labelWeather.Text = PySharp.ExecutePy(pyInterp, pyWeather);
}
}
但是,当我改变路径获取天气.py不在任意的随机位置,像这样:
^{pr2}$然后我的程序不再获得脚本的输出。这个脚本仍然有效:我使用IDLE启动它,它正确地完成了它的功能。当我使用C#调用它时,控制台打开了,但是没有得到任何输出。在
Python脚本如下:
from requests import get
from bs4 import BeautifulSoup as soup
r = get("http://www.ilmateenistus.ee/ilm/prognoosid/4-oopaeva-prognoos/")
parsed = soup(r.content, "html.parser")
container = parsed.find("div",{"class":"point kuusiku"})
print(str(container["data-title"]))
(它破坏了我当地的天气)
PySharp.ExecutePy() can be viewed here
到目前为止我遇到过的最奇怪的虫子。有什么想法吗?在
编辑1:似乎C#确实在读脚本中的某个东西。似乎这个东西是。。没有什么。我给了标签一个默认的示例文本,在运行程序之后,标签的文本被简单地更改为一个空字符串。希望这个不可思议的发现能有所帮助。在
编辑2:当脚本的文件路径包含空格时,程序无法正确调用该脚本。例如:
C:\foo bar\testing\pyWeather.py
不起作用!在
尝试用两个双引号将包含空格的路径括起来。在
例如
string pyWeather = @"""C:\Users\[myname]\Documents\Visual Studio 2017\Projects\testing\testing\scripts\getWeather.py""";
类似地,您可以在
string pyWeather = Directory.GetCurrentDirectory() + @"\scripts\getWeather.py";
后执行pyWeather = "\"" + pyWeather + "\"";
。在我希望你把答案退回而不是打印。打印机是一种基于I/O的显示解决方案。因此,它将在空闲时工作的非常好,但是它可能不会像您预期的那样返回结果。我坚信这将解决你的问题。 请尝试返回,而不是打印。我试过这个可以给更多的支持。
相关问题 更多 >
编程相关推荐