Python simple tips and common references

Source: Internet
Author: User
Tags json printf python script

Python Files support Chinese

#-*-Coding:utf-8-*-

Execute shell command

From subprocess import Popen, PIPE
def run_cmd (cmd):
#Popen Call Wrapper.return (code, stdout, stderr)
Child = Popen (cmd, stdin = PIPE, stdout = PIPE, stderr = PIPE, Shell = True)
Out, err = Child.communicate ()
ret = child.wait ()
Return (ret, out, err)

Gets the path of the current Python script file

Import OS
Os.path.split (Os.path.realpath (__file__)) [0]
Issues with the JSON module import

Try:
Import JSON
Except:
Import Simplejson as JSON

Format JSON with the JSON tool

#python less than 2.7
Echo\ ' {\ hello\ ': 1}\ ' | Python-m Simplejson.tool
#python 2.7 and above
Echo\ ' {\ hello\ ': 1}\ ' | Python-m Json.tool

General Call steps

Py_initialize (); Initializing the Python environment
Pyimport_importmodule ("test"); Load Python module
Pyobject_getattrstring (G_pmodule, "test1"); Get the pyobject of the corresponding Python function
Pyobject_callfunction (test1, "I,s", 2,e); Call Python's corresponding function
Py_finalize (); End

Example code for the C language

#include <python2.7/Python.h>


int main () {


Pyobject * g_pmodule = NULL;


py_initialize (); To invoke Py_initialize () before using Python, this function is initialized


if (! Py_isinitialized ())


        {


printf ("Init error\n");


return-1;


        }


pyrun_simplestring ("Import sys");


pyrun_simplestring ("sys.path.append (';/')");


g_pmodule =pyimport_importmodule ("mytest");//Here is the name of the file to invoke, we are here in the current directory test.py


if (!g_pmodule) {


printf ("Cant open python file!\n");


return-2;


        }


Pyobject * test1 = pyobject_getattrstring (G_pmodule, "test1");//Here is the name of the function to invoke


Pyobject *objresult = pyobject_callfunction (test1, "I,s", 2,e);//Call Function


if (!objresult) {


printf ("Invoke function fail\n");


        }


 


Pyobject * test2= pyobject_getattrstring (g_pmodule, "test2");//Here is the name of the function to invoke


Objresult = pyobject_callfunction (test2, "I", 2);//Call Function


char * x = pystring_asstring (Objresult);


printf ("%s\n", X);


py_finalize ()//Call py_finalize, this corresponds to Py_initialize.


}


Python program mytest.py

def test1 (S,STR):
Print S+str
return 0
def test2 (s):
return s

Compiling method of C program

#假设我们的python编译的时候安装在/opt/python, then we can use this command to compile the program.
$GCC-i/opt/python/include-l/opt/python/lib/-lpython2.7 test.c
Note: When Python compiles, it requires a dynamic-link library, plus--enable-shared

./configure--prefix=/opt/python--enable-shared

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.