午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Python簡(jiǎn)單技巧和常用參考
    來源:易賢網(wǎng) 閱讀:623 次 日期:2014-11-13 15:06:30
    溫馨提示:易賢網(wǎng)小編為您整理了“Python簡(jiǎn)單技巧和常用參考”,方便廣大網(wǎng)友查閱!

    python文件支持中文

    # -*- coding: UTF-8 -*-

    執(zhí)行shell命令

    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)

    獲取當(dāng)前python腳本文件所在路徑

    import os

    os.path.split(os.path.realpath(__file__))[0]

    json模塊 import的問題

    try :

    import json

    except :

    import simplejson as json

    使用json工具格式化json

    #python 2.7以下

    echo \'{\"hello\":1}\' | python -m simplejson.tool

    #python 2.7及以上

    echo \'{\"hello\":1}\' | python -m json.tool

    一般調(diào)用步驟

    Py_Initialize(); //初始化Python環(huán)境

    PyImport_ImportModule("test"); // 載入python模塊

    PyObject_GetAttrString(g_pModule,"test1"); //獲得相應(yīng)Python函數(shù)的PyObject

    PyObject_CallFunction(test1,"i,s",2,e); //調(diào)用Python相應(yīng)的函數(shù)

    Py_Finalize(); //結(jié)束

    C語言的示例代碼

    #include <python2.7/Python.h>

    int main(){

    PyObject * g_pModule = NULL;

    Py_Initialize(); //使用python之前,要調(diào)用Py_Initialize();這個(gè)函數(shù)進(jìn)行初始化

    if (!Py_IsInitialized())

    {

    printf("init error\n");

    return -1;

    }

    PyRun_SimpleString("import sys");

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

    g_pModule =PyImport_ImportModule("mytest");//這里是要調(diào)用的文件名,我們這里是當(dāng)前目錄下test.py

    if (!g_pModule) {

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

    return -2;

    }

    PyObject * test1 = PyObject_GetAttrString(g_pModule,"test1");//這里是要調(diào)用的函數(shù)名

    PyObject *objResult = PyObject_CallFunction(test1,"i,s",2,e);//調(diào)用函數(shù)

    if (!objResult){

    printf("invoke function fail\n");

    }

    PyObject * test2= PyObject_GetAttrString(g_pModule,"test2");//這里是要調(diào)用的函數(shù)名

    objResult = PyObject_CallFunction(test2,"i",2);//調(diào)用函數(shù)

    char * x = PyString_AsString(objResult);

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

    Py_Finalize();//調(diào)用Py_Finalize,這個(gè)跟Py_Initialize相對(duì)應(yīng)的。

    }

    Python程序mytest.py

    def test1(s,str):

    print s+str

    return 0

    def test2(s):

    return s

    C程序的編譯方法

    #假設(shè)我們的python編譯的時(shí)候安裝在/opt/python里,那么我們可以用這樣的命令來編譯程序

    $gcc -I/opt/python/include -L/opt/python/lib/ -lpython2.7 test.c

    注意: 這里要求python編譯的時(shí)候,需要有動(dòng)態(tài)鏈接庫即加上--enable-shared

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

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:Python簡(jiǎn)單技巧和常用參考
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)