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

熱線電話:13121318867

登錄
首頁精彩閱讀Python中字符串中的數(shù)字提取方法
Python中字符串中的數(shù)字提取方法
2017-06-28
收藏

Python中字符串中的數(shù)字提取方法

逛到一個有意思的博客 在里面看到一篇關(guān)于ValueError: invalid literal for int() with base 10錯誤的解析,針對這個錯誤,博主已經(jīng)給出解決辦法,使用的是re.sub 方法

1 totalCount = '100abc'
2 totalCount = re.sub("\D", "", totalCount)

但是沒有說明什么含義,于是去查了其他的資料,做一下記錄:

在Python3.5.2 官方文檔re模塊中sub函數(shù)的定義是:

re.sub(pattern, repl, string, count=0, flags=0)

在字符串 string 中找到匹配正則表達(dá)式 pattern 的所有子串,用另一個字符串 repl 進(jìn)行替換。如果沒有找到匹配 pattern 的串,則返回未被修改的 string。Repl 既可以是字符串也可以是一個函數(shù)。

由此可分析上面使用的語句的含義:在'100abc'這個字符串中找到非數(shù)字的字符(正則表達(dá)式中'\D'表示非數(shù)字),并用""替換,然后返回的就是只剩下數(shù)字的字符串。

>>> totalCount = '100abc'
>>> totalCount = re.sub("\D", "", totalCount)
>>> print(totalCount)
100
>>> type(totalCount)
<class 'str'>

好吧,以上說明完畢,不過其實(shí)我想到的是我爬取知乎所關(guān)注的問答時,所遇到的類似的問題:

1 answer_num_get = soup.find('h3', {'id': 'zh-question-answer-num'})   # 答案數(shù)量:32 個回答
2 if answer_num_get is not None:
3     answer_num = int(answer_num_get.split()[0])
4 n = answer_num // 1

其中第三行之所以能用int(),是因?yàn)閟tring.split()[0]將answer_num_get的值“32 個回答”提取出數(shù)字(注:32后面有一個空格,在這里非常重要,因?yàn)橹跎献ト』貋淼倪@個元素就是)

split()的定義    str.split(sep=None, maxsplit=-1)

>>> import string
>>> a = "32 個答案"
>>> b = a.split()[0]
>>> print(b)
32
>>> type(b)
<class 'str'>
>>> c = '1,2,3'
>>> c.split(',')
['1', '2', '3']
>>> c.split(',')[0]
'1'
>>> c.split(',')[1]
'2'
>>>

由此可看出split()的第一個參數(shù)是分隔符,如果什么都不填就是默認(rèn)是以空格來分隔。

第一種方法需要用到正則表達(dá)式,第二種方法則需要有分隔符(我猜是不是這個原因,在原網(wǎng)頁上總答案數(shù)的數(shù)字后有個空格存在)。  這兩種方法都有點(diǎn)局限性,不知道是否有更好的方法來分離字符串中的數(shù)字。

數(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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗(yàn)證碼的宕機(jī) 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){ //倒計(jì)時完成 $(".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); }