我正在尝试从C#应用程序中运行python脚本。尝试在常规命令提示符下运行脚本时,找不到库的问题,但是当我从Anaconda命令提示符下运行脚本时,它们可以工作。如何从C#应用程序中启动Anaconda命令提示符?
所以,我有这段代码可以正常工作,但是(总是有一个but)现在我想知道如何获取我试图检索的实际输出。
string pythontempPath = Properties.Settings.Default.TempPath;
var workingDirectory = Path.GetFullPath(pythontempPath);
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = workingDirectory
}
};
process.Start();
using (var sw = process.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(@"C:\ProgramData\Anaconda3\Scripts\activate.bat");
sw.WriteLine("python " + pythontempPath + @"\" + pythonFilePath + " -f " + "\"" + excelFilePath + "\"");
}
}
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd();
string result = reader.ReadToEnd();
Console.WriteLine("{0}", result);
}
在这里,打印结果为: C:\ Users> C:\ ProgramData \ Anaconda3 \ Scripts \ activate.bat
(基本)C:\ Users> python C:\ Users \ GetDataHeaders.py -f“ KNN_Project_Data” ['XVPM','GWYH','TRAT','TLLZ','IGGA','HYKR','EDFS','GUUB','MGJM','JHZC','TARGET CLASS']
(基本)C:\ Users>
我实际上只是想要: ['XVPM','GWYH','TRAT','TLLZ','IGGA','HYKR','EDFS','GUUB','MGJM','JHZC','TARGET CLASS']
除了丢弃以(base)开头的任何行以外,还有没有办法从python脚本中获取实际输出?还是做我想做的更好的方法?