import csv import re import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.label import Label from kivy.uix.gridlayout import GridLayout from kivy.uix.textinput import TextInput # dirty start to adding a GUI using kivy class LoginScreen(GridLayout): def __init__(self, **kwargs): super(LoginScreen, self).__init__(**kwargs) self.cols = 2 self.add_widget(Label(text='User Name')) self.username = TextInput(multiline=False) self.add_widget(self.username) self.add_widget(Label(text='password')) self.password = TextInput(password=True, multiline=False) self.add_widget(self.password) class csv_cleaner(App): def build(self): return LoginScreen() 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() for index in extra_item_index_dict[extra_item_number + 1]: item = raw_input_row_list[index] new_row.append(item) output_csv_list.append(new_row) with open ('./LWZ_export.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) # 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 extra_item_number, extra_item in enumerate(extra_item_index_list): if row[extra_item] == "Yes": create_row(row, extra_item_number, extra_item_index_dict, course_info, output_csv_list) else: break with open("./output.csv", "w", encoding="utf8") as output_file: write = csv.writer(output_file) write.writerows(output_csv_list) if __name__ == '__main__': csv_cleaner().run()