170k views
5 votes
I am currently working on a python project that requires my application to parse a court calendar text file, extracts relevant information on defendants and write said info to a .csv file. Here is my code thus far as well as an example of what its supposed to look like after the program runs. I need help on where to go next, how can I get it to write to the .csv. Any help would be greatly appreciated.

END_HEADER = '*' * 30
END_DEFEND = 'BOND'

def is_defend_data(line): #tells t/f if it is a defendant line
try:
int(line[0:6])
return True
except ValueError:
return False


def process_defend_data(line, infile):
data = {}
data['DefendData'] = line[0:85].strip()

while True:
line = infile.readline()
if END_DEFEND in line:
break


def is_charge_data(line):
return "PLEA:" in line

def process_charge_data(line):
data = {}
data['Charge'] = line[8:45].strip()
data['Plea'] = line[49:65].strip()
data['Verdict'] = line[65:].strip()

def is_report_header(line):
return line[0] != '1' and 'RUN DATE:' in line

def process_report_header(line, infile):
data = {}
data['RunDate'] = line[12:20].strip()

while True:
line = infile.readline()
if END_HEADER in line:
break
elif 'TIME' in line:
data['Date'] = line[22:30].strip()
data['Time'] = line[44:52].strip()
data['Room'] = line[78:].strip()
return data

def is_page_header(line):
return line[0] == '1' and 'RUN DATE:' not in line


def process_page_header(line, infile):
while True:
line = infile.readline()
if END_HEADER in line:
break

def is_bond_data(line):
return 'BOND' in line

def process_bond_data(line):
data = {}
data['Bond'] = line[20:30].strip()

def is_fingerprint_data(line):
return "DEFENDANT NEEDS TO BE FINGERPRINTED" in line

def process_fingerprint_data(line):
data = {}
data['fingerprint'] = True
return data

def is_summary_header(line):
return line[0] == '1' and 'RUN DATE:' in line


def main():
session_data = {}
defend_data = {}
charge_data ={}

# filename = input("Enter filename: ")
filename = 'DISTRICT.DISTRICT_COURT_.04.11.23.AM.9999.CAL.txt'

infile = open(filename, 'r')

while True:
line = infile.readline()

if line == "" or is_summary_header(line):
break
elif line == '\\':
pass
elif is_page_header(line):
process_page_header(line, infile)
elif is_report_header(line):
session_data = process_report_header(line, infile)
elif is_defend_data(line):
defend_data = process_defend_data(line, infile)
elif is_charge_data(line):
charge_data = process_charge_data(line)
elif is_bond_data(line):
bond_data = process_bond_data(line)
elif is_fingerprint_data(line):
fingerprint_data = process_fingerprint_data(line)
else:
print(line)

infile.close()

if __name__ == "__main__":
main()

User Pawel Kam
by
7.8k points

1 Answer

0 votes

An example of how you can modify your code is shown below

Add the following import at the beginning of your script:

python

import csv

To Modify your main function to create and write to a CSV file, the code is:

python

def main():

session_data = {}

defend_data = {}

charge_data = {}

# filename = input("Enter filename: ")

filename = 'DISTRICT.DISTRICT_COURT_.04.11.23.AM.9999.CAL.txt'

infile = open(filename, 'r')

# Create a CSV file for writing

with open('output.csv', 'w', newline='') as csvfile:

fieldnames = ['RunDate', 'Date', 'Time', 'Room', 'DefendData', 'Charge', 'Plea', 'Verdict', 'Bond', 'Fingerprint']

writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

# Write the header row to the CSV file

writer.writeheader()

while True:

line = infile.readline()

if line == "" or is_summary_header(line):

break

elif line == '\\':

pass

elif is_page_header(line):

process_page_header(line, infile)

elif is_report_header(line):

session_data = process_report_header(line, infile)

elif is_defend_data(line):

defend_data = process_defend_data(line, infile)

elif is_charge_data(line):

charge_data = process_charge_data(line)

elif is_bond_data(line):

bond_data = process_bond_data(line)

elif is_fingerprint_data(line):

fingerprint_data = process_fingerprint_data(line)

else:

print(line)

# Write the data to the CSV file

writer.writerow({

'RunDate': session_data.get('RunDate', ''),

'Date': session_data.get('Date', ''),

'Time': session_data.get('Time', ''),

'Room': session_data.get('Room', ''),

'DefendData': defend_data.get('DefendData', ''),

'Charge': charge_data.get('Charge', ''),

'Plea': charge_data.get('Plea', ''),

'Verdict': charge_data.get('Verdict', ''),

'Bond': bond_data.get('Bond', ''),

'Fingerprint': fingerprint_data.get('fingerprint', False),

})

infile.close()

if __name__ == "__main__":

main()

What is the code?

The above code change includes a CSV writer that saves the data into a file called output. csv. So, to use it, do change the name and location of the file if necessary.

Therefore, The csvDictWriter class is used to write dictionaries to a CSV file, and the writeheader() method is used to write the first row as the header. The writerow() function writes one row of data to a CSV file.

User RSATom
by
7.3k points