You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.5 KiB

import csv
import re
# current working script, runs from command line only
def create_row(raw_input_row_list, extra_item_number, extra_item_index_dict, course_info_list, output_csv_list):
# creates new row for extra items
new_row = course_info_list.copy()
# print(course_info_list)
# print(extra_item_index_dict[extra_item_number + 1])
for index in extra_item_index_dict[extra_item_number + 1]:
# print(index)
# print(new_row)
item = raw_input_row_list[index]
# print(item)
new_row.append(item)
# print(new_row)
output_csv_list.append(new_row)
with open ('./report.csv', encoding="utf8") as input_csv:
input_reader = csv.reader(input_csv, delimiter=',')
data = list(input_reader)
heading_row = data[0]
extra_item_index_list = []
for index, heading in enumerate(data[0]):
if "Another" in heading:
extra_item_index_list.append(index)
empty_column_headers = ["Status", "File Name", "Notes"]
course_details_list = ["Length of Reserve", "Full Name", "Course Number", "Course Title"]
item_details = ["Material", "Author", "Title:", "Page", "Chapter"]
course_details_index_list = []
for course_detail in course_details_list:
for index, heading in enumerate(data[0]):
if (re.search(course_detail, heading) and (index not in course_details_index_list)):
course_details_index_list.append(index)
break
extra_item_index_dict = {}
item_details_index_list = []
# need to split up item headings by item number
#
for index, heading in enumerate(data[0]):
for item_detail in item_details:
# needs handling for #10
if (re.search("[1-9]*" + item_detail, heading) and (index not in item_details_index_list) and (index not in course_details_index_list) and ("Journal" not in heading)):
item_details_index_list.append(index)
# break
# extra_item_index_dict.update()
# split extra item index ref list into chunks by item and add to dictionary
x = 0
item_number = 1
for i in range(x,len(item_details_index_list),len(item_details)):
x = i
extra_item_index_dict.update({item_number:item_details_index_list[x:x+5]})
item_number += 1
output_csv_list = []
output_csv_list.append(empty_column_headers + course_details_list + item_details)
# need to create function for new row/item creation. func must take in course row data along with indices of the next item info and/or the item data in a list. row parsing could be separate from new row creation?
for row in data[1:]:
course_info = []
new_row_first = []
for item in empty_column_headers:
new_row_first.append("")
course_info.append("")
for index in course_details_index_list:
course_detail = row[index]
course_info.append(course_detail)
new_row_first.append(course_detail)
# for index in extra_item_index_dict[1]:
# new_row_first.append(row[index])
# print(new_row_first)
for extra_item_number, extra_item in enumerate(extra_item_index_list):
if row[extra_item] == "Yes":
# print(extra_item_number)
create_row(row, extra_item_number, extra_item_index_dict, course_info, output_csv_list)
else:
break
# output_csv_list.append(new_row)
# output_csv_list.append(new_row_first)
# for i in item_details_index_list:
# print(data[0][i])
with open("./output.csv", "w", encoding="utf8", newline='') as output_file:
write = csv.writer(output_file)
write.writerows(output_csv_list)