99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python調(diào)用ctypes使用C函數(shù)printf的方法
Python調(diào)用ctypes使用C函數(shù)printf的方法
2017-09-26
收藏

Python調(diào)用ctypes使用C函數(shù)printf的方法

Python程序中導入ctypes模塊,載入動態(tài)鏈接庫。動態(tài)鏈接庫有三種:cdll以及windows下的windll和oledll,cdll載入導出函數(shù)使用標準的cdecl調(diào)用規(guī)范的庫,而windll載入導出函數(shù)符合stdcall調(diào)用規(guī)范(Win32 API的原生約定)的庫,oledll也使用stdcall調(diào)用規(guī)范,并假設函數(shù)返回Windows的HRESULT錯誤代碼。錯誤代碼用于在出錯時自動拋出WindowsError這個Python異常,可以使用COM函數(shù)得到具體的錯誤信息。

使用cdll.msvcrt即可調(diào)用MS標準的C庫msvcrt,msvcrt包含了大部分標準C函數(shù)。

下面來看一下簡單的printf函數(shù)。    
from ctypes import *
msvcrt = cdll.msvcrt
str = "Huanhuan!"
msvcrt.printf("Hello %s\n", str)

這樣就可以使用C語言中的printf函數(shù)進行輸出。
如果在IDLE里運行的話會發(fā)現(xiàn)程序沒有任何輸出結(jié)果,這是因為printf是打印到真實的標準輸出,而不是sys.stdout。如果想要看到運行結(jié)果,可以在CMD里運行python test.py來查看結(jié)果,前提是已經(jīng)設置好了Python的環(huán)境變量。或者有一個曲線方法可以在IDLE中顯示輸出結(jié)果,請曲線閱讀到文章最后。

如果使用的是Py3K,在控制臺里會看到只有開頭字符H被輸出了。因為Py3K使用的是Unicode編碼,而printf不支持該編碼,所以需要轉(zhuǎn)碼。整理出來三種改寫方法可以解決這一問題。
    
# A 轉(zhuǎn)為byte類型 在字符串前面加b
from ctypes import *
msvcrt = cdll.msvcrt
str = b"Huanhuan!"
msvcrt.printf(b"Hello %s\n", str)
 
# B 使用wprintf寬字符顯示
from ctypes import *
msvcrt = cdll.msvcrt
str = "Huanhuan!"
msvcrt.wprintf("Hello %s\n", str)
 
# C 轉(zhuǎn)碼為utf-8
from ctypes import *
msvcrt = cdll.msvcrt
str = "Huanhuan!"
result = "Hello " + str + "\n"
result = result.encode("utf-8")
msvcrt.printf(result)

最后來搞定在IDLE中曲線顯示輸出結(jié)果的方法。    
from ctypes import *
msvcrt = cdll.msvcrt
str = b"Huanhuan!"
s = create_string_buffer(100)  # 必須足夠長
msvcrt.sprintf(s, b'Hello %s\n', str)
print(s.value.decode('utf-8'))

先使用sprintf函數(shù)把結(jié)果輸出到s變量,然后再用Python自帶的print方法輸出s的value。

好了,通過以上的各種方法就可以解決Py3K調(diào)用C函數(shù)printf的問題了。

什么?你問我為什么費這么大勁非要用printf輸出,而不是直接用Python自帶的print?

python的print和c的printf有什么區(qū)別

print([object, ...], *, sep=' ', end='\n', file=sys.stdout, flush=False)

輸出對象到流文件,sep指定分割符,end指定結(jié)束符。參數(shù)轉(zhuǎn)換為字符串寫入輸出流,如果沒有輸出內(nèi)容直接輸出end結(jié)束符。file參數(shù)必須是包含write方法的對象,默認輸出到標準輸出。

int printf( char * format, ... );

根據(jù)參數(shù) format 字符串來轉(zhuǎn)換并格式化數(shù)據(jù),然后將結(jié)果輸出到標準輸出設備(顯示器),直到出現(xiàn)字符串結(jié)束('\0')為止。
參數(shù) format 字符串可包含下列三種字符類型:

一般文本,將會直接輸出
ASCII 控制字符,如\t、\n 等有特定含義
格式轉(zhuǎn)換字符

格式轉(zhuǎn)換為一個百分比符號(%)及其后的格式字符所組成。一般而言,每個%符號在其后都必需有一個參數(shù)與之相呼應(只有當%%轉(zhuǎn)換字符出現(xiàn)時會直接輸出%字符),而欲輸出的數(shù)據(jù)類型必須與其相對應的轉(zhuǎn)換字符類型相同。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }