# -*- 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)

20190419代码学习备份更多用法参见网址: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)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
  • 2022-01-20
  • 2021-11-22
  • 2021-12-19
  • 2022-01-06
猜你喜欢
  • 2021-08-14
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案