C # calling Python

Source: Internet
Author: User

Resources:
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";    //文件路径+参数集合    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();    //取出计算结果    Console.WriteLine(output[0]);    Console.Read();

Import Base64Import HMACImport HashlibImport Urllibimport urlparseimport json Import Urllibimport sysdef Span class= "Hljs-title" >_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 ()           

It is important to note that:
(1) The Python version of the example is 2.7 and can be used Python3, which is related to your environment
Learning process encountered in the more worried problem is "Python2 can not find Python3 library, method" and so on, to ensure that the environment is consistent and correct AH ╮ (╯_╰) ╭
(2) Python basic syntax problem, indentation is either a space, or tab, consistent, otherwise vs will error

C # calling Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.