import csv
import numpy as np

with open('Rick-n-Morty.csv', newline='', encoding='utf-8') as csvfile:
    csv_data = csv.reader(csvfile, delimiter=',', quotechar='"') #read the csv data with the correct format
    data = [] 
    for row in csv_data:
        data.append(row) #then turn the csv data into a list

rick_names = ['Rick', 'Rick:', 'Rick: ', 'Rick (sarcastic)', 'Rick: *',
              'Rick ', 'Rick and Needful', 'Rick (phone)', 'Pickle Rick',
              'RIck', 'Toxic Rick']

# I kept the number in front of the line just to check how the analysis went across the database, then I got rid of it.
rick_only_nb = []
for i in range(np.shape(data)[0]):
    if data[i][2] in rick_names:
        rick_only_nb.append(data[i][:])

#Cleaning process
clean_rick_only_nb = []
for i in range(len(rick_only_nb)):
    new_line = ''
    for j in rick_only_nb[i][3]:
        if (j != '\n'):
            new_line += j
    if new_line[0:2] == ": ":
        new_line = new_line[2:]
    if (new_line != '') and (new_line != '['):
        clean_rick_only_nb.append(rick_only_nb[i][:2]+['Rick']+[' '.join(new_line.split())])

with open('rick_test_nb.csv', 'w', newline='', encoding='utf-8') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ', quoting=csv.QUOTE_MINIMAL)
    for line in clean_rick_only_nb:
        spamwriter.writerow(line)