yjhfgdfd 发表于 2016-7-11 09:04:29

Python下用List对员工信息表进行模糊匹配

#需求

[*]用户可以模糊查询员工信息

[*]显示匹配了多少条,匹配字符需要高亮度显示


#脚本内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
while True:
      info = 'info.txt'
      f = file(info)
      search=raw_input('Please Engter You Search Info: ')
      for line in f.readlines():
      i=line.strip().split()
      q=i
      w=i
      e=i
      r=i
      g=len(search)

      #第1列文件模糊匹配
      if search in q:   
            d=len(q)
            c=q.find(search)
            j=c+g
            if c == 0:
            u=q[:g]
            o=q
            print"\033[31m%s\033[0m%s %s %s%s"% (u,o,w,e,r)
      elif j == d:
            u=q[:c]
            o=q
            print"%s\033[31m%s\033[0m %s%s%s"% (u,o,w,e,r)
      else:
            u=q[:c]
            o=q
            p=q
            print"%s\033[31m%s\033[0m%s %s %s%s"% (u,o,p,w,e,r)

      #第2列文件模糊匹配
      if search in w:   
                d=len(w)         
                c=w.find(search)
                j=c+g
                if c == 0:
                  u=w[:g]
                  o=w
                  print"%s\033[31m %s\033[0m%s%s%s"% (q,u,o,e,r)
                elif j == d:
                  u=w[:c]
                  o=w
                  print"%s %s\033[31m%s\033[0m%s%s"% (q,u,o,e,r)
                else:
                  u=w[:c]
                  o=w
                  p=w
                  print"%s %s\033[31m%s\033[0m%s%s%s"% (q,u,o,p,e,r)

      #第3列文件模糊匹配
      if search in e:         
                d=len(e)
                c=e.find(search)
                j=c+g
                if c == 0:
                  u=e[:g]
                  o=e
                  print"%s %s\033[31m %s\033[0m%s%s"% (q,w,u,o,r)
                elif j == d:
                  u=e[:c]
                  o=e
                  print"%s %s %s\033[31m%s\033[0m%s"% (q,w,u,o,r)
                else:
                  u=e[:c]
                  o=e
                  p=e
                  print"%s %s %s\033[31m%s\033[0m%s%s"% (q,w,u,o,p,r)

      #第4列文件模糊匹配
      if search in r:
            d=len(r)
            c=r.find(search)
            j=c+g
            if c == 0:
                u=r[:g]
                o=r
                print"%s %s %s\033[31m %s\033[0m%s"% (q,w,e,u,o)
            elif j == d:
                u=r[:c]
                o=r
                print"%s %s %s %s\033[31m%s\033[0m"% (q,w,e,u,o)
            else:
                u=r[:c]
                o=r
                p=r
                print"%s %s %s %s\033[31m%s\033[0m%s"% (q,w,e,u,o,p)





#员工信息表展示

1
2
3
4
5
# cat info.txt
wsyht1315326095 yaowan t891672832@126.com
peter1823572871 duowan t218460931@163.com
jack   15832908124 tanwan t679312053@139.com
jenkis 17937829012 haowan t357891241@qq.com





#脚本执行展示



页: [1]
查看完整版本: Python下用List对员工信息表进行模糊匹配