# -*- coding: utf-8 -*-
"""
Created on Fri Apr 19 09:39:54 2019
@author: pt0531
"""
import numpy as np
import pandas as pd
import re
p1=open(r"C:\Users\pt0531\Desktop\data1.csv",encoding='gbk',errors='ignore')
#encoding和errors='ignore'这一块一直疯狂报错,继续学习!!!!!!!!
data=pd.read_csv(p1,dtype=str) #默认header=0从第一行提取列名
#这样处理防止文件路径中有中文而报错
#zonglenth=1048575
datalst=(np.array(data))
datalst=datalst.tolist()
tongji=[]
for i in range(len(datalst)):
for j in range(len(datalst[i])):
tongji.append(datalst[i][j])
#整理成这样tongji=['','','','']
len(tongji)
count=0
for i in range(len(tongji)):
m=re.match(r'^[a-zA-Z]{3}\d{2}.*',str(tongji[i]))
if m:
count+=1
count#41338
bili=count/zonglenth
bili#0.0394
#此处关于if m的用法:
一个重要的学习网址:https://docs.python.org/zh-cn/3/library/re.html
re.match(pattern, string, flags=0)
如果 string 开始的0或者多个字符匹配到了正则表达式样式,就返回一个相应的 匹配对象 。 如果没有匹配,就返回 None ;注意它跟零长度匹配是不同的。
注意即便是 MULTILINE 多行模式, re.match() 也只匹配字符串的开始位置,而不匹配每行开始。
如果你想定位 string 的任何位置,使用search() 来替代(也可参考search() vs .match())
FurtherMore,学习匹配对象是个啥:
匹配对象总是有一个布尔值 True。如果没有匹配的话 match() 和 search() 返回 None 所以you can 简单的用 if 语句来判断是否匹配
match = re.search(pattern, string)
if match:
process(match)
更多用法参见网址:https://docs.python.org/zh-cn/3/library/re.html#match-objects
zonglenth=1048575
len(tongji)
count=0
for i in range(len(tongji)):
m=re.match(r'^[a-zA-Z]{3,}\d{2,}.*',str(tongji[i]))
if m:
count+=1
count#136894
bili=count/zonglenth
bili#0.13055
requirement:要稍微正规一点,把抽取条件写清楚,数目和占比分别列出来,做成表格
#自己的学习
liebiao=[]
count=0
for i in range(50):
m=re.match(r'^[a-zA-Z]{3}\d{2}.*',str(tongji[i]))
if m:
count+=1
print(m)