0

I have a problem with my Python code. I loaded a csv file into python using the csv module (as a list of lists), then did some cleaning procedures with it (that's the function 'cleaning', it takes two lists: the original and the edited one), and then wanted to print it. When printing immediately after cleaning a single entry (a single list), everything works fine. However, after printing the whole cleaned dataset, all the last entries in that set are the same (which they are not suppossed to be). Where am I going wrong?

   import csv

total_list = [0] * 2809
copy_list2 = [0] * 9
copy_list = [copy_list2] * 2808
#Cleaning procedure
def cleaning(member_list, copy_list):
    #If last name is entered in first name column, moves last part of
    #first name to last name and removes it from first name (if first name)
    #contains multiple words
    for i in range(len(member_list)):
        copy_list[i] = member_list[i]
    if len(member_list[2]) == 0 and len(member_list[1].split(" ")) > 1:
        copy_list[2] = member_list[1].split(" ")[-1]
        copy_list[1] = " ".join(member_list[1].split(" ")[:-1])
    #Change all empty entries to Nb
    for i in range(len(member_list)):
        if len(str(copy_list[i])) == 0:
            copy_list[i] = "Nb"
    #Capitalize first letter, and lower all other letters
    for i in range(len(copy_list[:6])):
        if copy_list[i].isupper():
            copy_list[i] = copy_list[i][0].upper() + copy_list[i][1:].lower()
    #Change seperation operator of dates to /
    for i in range(6,9):        
        if "." in member_list[i]:
            copy_list[i] = member_list[i].replace(".", "/")
        elif "-" in member_list[4]:
            copy_list[i] = member_list[i].replace("-", "/")


#Open csv File
with open('Archive.csv', 'r') as csvfile:
    spamreader = csv.reader(csvfile, delimiter = ';', quotechar = '|')
    i = 0
    for row in spamreader:
        total_list[i] = row
        i += 1   
#Delete header row
del total_list[0]
i = 0
for j in total_list:
    cleaning(j, copy_list[i])
    #When printing j and copy_list[i] everything looks fine
    i += 1

#When printin here, suddenly the last elements are all the same!              
print(copy_list[-3])
JPR
  • 13
  • 3

0 Answers0