I am writing code to clean a very large data set of arrest records. I have been using regex to do this.
really confused and I couldn't really figure out anything from other questions. Also tried to run it in pythonanywhere and it still did not work
i tried importing regex as re but i got this error
e-packages\regex\regex.py", line 546, in _compile
raise error(caught_exception.msg, caught_exception.pattern,
regex._regex_core.error: missing < at position 9
#arresttrasnfo
import pandas as pd
import xlsxwriter
import re
#take single line of arrest data all garbled turn it into county - date of arrest and crime or code (as best as possible)
def countiesfunc(locdata):
d1 = "(\((.*?)\))" # catches stuff in parentheses
red1 = re.compile(d1)
counties = []
for item in locdata:
search = (red1.search(item))
if search == None:
counties.append("No County Given or Failed to Parse County")
else:
counties.append(search)
return counties
def datesfunc(timedata):
d1 = '((\d|\d\d)(\/|\.)(\d|\d\d)(\/|\.)(\d\d|\d))' #inclusive date pattern
red1 = re.compile(d1)
dates = []
for item in timedata:
search = (red1.search(item))
if search == None:
dates.append("Failed to Parse Date")
else:
dates.append(search)
return dates
def crimesfunc(crimedata):
d1 = "(\-\s\P\L(.*?)\,)|(\/(.*?)\-)|(\d\s(.*?)\-)" #inclusive formula to catch all crimes
red1 = re.compile(d1)
crimes = []
for item in crimedata:
search = (red1.search(item))
if search == None:
crimes.append("Failed to Parse Date")
else:
crimes.append(search)
def ziptodf(data,dates,counties,crimes):
mapped = zip(data,dates,counties,crimes)
resultdf = pd.DataFrame(mapped, columns = ['ACTN_CONDITN','Arrest Date','Counties',"Crime"])
resultdf.to_excel("results.xlsx",engine = 'xlsxwriter')
print('DataFrame is written to Excel File successfully.')
def main():
df = pd.read_csv('arrestinfo.csv')
data = df['ACTN_CONDITN'].tolist()
counties = countiesfunc(data)
crimes = crimesfunc(data)
dates = datesfunc(data)
ziptodf(data,dates,counties,crimes)
main()
I keep getting this error re.error: bad escape \P at position 5