32.9k views
0 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.

My code so far:

import csv

END_HEADER = "*" * 30
#COLUMN_NAMES = ['first_name', 'last_name']

def is_defend_data(line):
try:
int(line[0:6])
return True
except ValueError:
return False

def process_defend_data(line):
data = {}

data['Number'] = line[0:6].strip()
data['File Number'] = line[8:20].strip()
data['Defendent Name'] = line[20:42].strip()
data ['Complainant'] = line[42:61].strip()
data['Attorney'] = line[41:85].strip()
data['Cont'] = line[84:].strip()

return data


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()

return data


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 is_AKA_data(line):
return "AKA" in line

def process_AKA_data(line):
data = {}
data['AKA'] = line[43:74].strip()

def is_judgment_data(line):
return "JUDGMENT" in line

def process_judgment_data(line):
data = {}
data['Class'] = line[12:16].strip()
data['P:'] = line[17:21].strip()
data['L:'] = line[22:41].strip()
data['Judgment'] = line[49:77].strip()

def main():
session_data = {}
defend_data = {}
charge_data = {}
bond_data = {}
fingerprint_data = {}
judgment_data = {}
AKA_data = {}

# filename = input("Enter filename: ") needs to be in final program
filename = "DISTRICT.DISTRICT_COURT_.04.10.23.AM.000B.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)
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)
elif is_judgment_data(line):
judgment_data = process_judgment_data(line)
elif is_AKA_data(line):
AKA_data = process_AKA_data(line)
else:
print(line.rstrip())

infile.close()

#outfile = open('names.csv', 'w', newline='')
#writer = csv.DictWriter(outfile, fieldnames = COLUMN_NAMES)

#writer.writeheader()

#writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
#writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})

if __name__ == "__main__":
main()

I am currently working on a python project that requires my application to parse a-example-1
User Fabianbru
by
8.7k points

1 Answer

4 votes

Answer: To write the information to a CSV file, you can follow these steps:

1. Open the CSV file for writing.

2. Create a `csv.DictWriter` object using the opened file and specify the field names.

3. Write the header using the `writeheader()` method.

4. Write the rows of data using the `writerow()` method.

Here's how you can modify your code to include these steps:

```python

import csv

# ... (existing code)

COLUMN_NAMES = ['Number', 'File Number', 'Defendent Name', 'Complainant', 'Attorney', 'Cont',

'Charge', 'Plea', 'Verdict', 'RunDate', 'Date', 'Time', 'Room', 'Bond', 'Fingerprint',

'Class', 'P:', 'L:', 'Judgment', 'AKA']

# ... (existing code)

def main():

# ... (existing code)

# Open the CSV file for writing

with open('court_data.csv', 'w', newline='') as outfile:

writer = csv.DictWriter(outfile, fieldnames=COLUMN_NAMES)

# Write the header

writer.writeheader()

# Write the rows of data

writer.writerow({**session_data, **defend_data, **charge_data, **bond_data, **fingerprint_data, **judgment_data, **AKA_data})

if __name__ == "__main__":

main()

```

This assumes that `COLUMN_NAMES` includes all the keys used in the various data dictionaries. The `**` syntax is used to merge the dictionaries into a single dictionary for writing to the CSV file.

Please adjust the code as needed based on your specific requirements and the structure of your data.

Step-by-step explanation:

User David Burg
by
8.0k points