-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex_test.py
42 lines (36 loc) · 1 KB
/
regex_test.py
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
"""
Thur Oct 31, 2019
Stacy Bridges
"""
import re, os, sys
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import numpy as np
# get input file
folder_path = os.path.dirname(os.path.abspath(__file__))
infile = folder_path + '\\db_data_org_electrical_short_wx_v1.xlsx'
outfile = folder_path + '\\api_in_ids.csv'
# get dataframe/column
df_tender = pd.read_excel(infile, sheet_name=0) # read file into dataframe
mpns = df_tender['ManufacturerPartNo']
# identify search pattern
RS_regex='\d{3}[-]\d{3,4}'
# print search results to console
results = []
for row in mpns:
results.append(re.findall(RS_regex,str(row)))
with open(outfile, 'w') as ofile:
ofile.write('RS Code')
ofile.write('\n')
id_count = 0
for i in results:
if len(i)>0:
id_count += 1
print(i[0])
ofile.write(i[0])
ofile.write('\n')
else:
ofile.write('\n')
print('{} codes extracted and written to:'.format(id_count))
print(outfile)