C # Call python,
References:
Https://codefying.com/2015/10/02/executing-a-python-script-from-a-c-program/
Demo:
Https://github.com/zLulus/NotePractice/tree/dev3/Console/CSharpUsingPythonDemo
Code
String progToRun = "test. py "; char [] spliter = {'\ R'}; Process proc = new Process (); proc. startInfo. fileName = "python.exe"; proc. startInfo. redirectStandardOutput = true; proc. startInfo. useShellExecute = false; string psw = "123456"; string parameters2 = "haha"; // file path + parameter set proc. startInfo. arguments = string. concat (progToRun, "", psw. toString (), "", parameters2.ToString (); proc. start (); StreamReader sReader = proc. standardOutput; string [] output = sReader. readToEnd (). split (spliter); foreach (string s in output) Console. writeLine (s); proc. waitForExit (); // retrieves the calculation result Console. writeLine (output [0]); Console. read ();
import base64import hmacimport hashlibimport urllibimport urlparseimport jsonimport urllibimport sysdef _auth_data(): psw=sys.argv[1] parameters2=sys.argv[2] md5 = hashlib.md5() md5.update(psw.encode('utf-8')) md5.update('hello'.encode('utf-8')) s = json.dumps({"assetPwd": md5.hexdigest()}) r=urllib.quote(s, safe='') print r return rif __name__ == '__main__': _auth_data()
Note that:
(1) The python version of the example is 2.7. You can use python3, which is related to your environment.
The tough problem encountered during the learning process is also the problem of "python2 cannot find the library and method of python3", which ensures that the environment is consistent and correct.
(2) for basic python Syntax problems, indentation can be either a space or a tab to maintain consistency. Otherwise, VS will report an error.