So let me clear the air, YES this for an assignment do please do not spoon feed me. Points in the right direction would be good.
Here is what I'm doing.
Essentially I have two .CSV files. Lets say they contain this data. A combination of names and test scores.
File1: <--- base names file
john,,,,
steve,,,,
michael,,,,
alfred,,,,
File2: <--- A file with marks from certain people
john,2,,,
steve,9,,,
jessica,5,,,
bill,1,,,
alfred,3,,,
The gist is, I am supposed to make a new file combining the two. HOWEVER, since file2 has instances which are not in file1 (i.e jessica and bill), they do not get added to the new .csv file and are instead left out.
The final third .csv file which is what I am tasked to make should look like this
File3: <--- File I make
john,2,,,
steve,9,,,
michael,,,, <-- even though it didn't receive input from the other file, it remains
alfred,3,,,
I am fairly sure I am 99% of the way there, here is my current code. Yes it is going to look nooby but I have been experimenting with stuff all afternoon, even some ludicrously nubbish ideas which I knew from the start wouldn't work but I tried anyways. Note I haven't actually tried to make the third file, I just want to be able to OUTPUT what it should look like first. One problem at a time ^.^
import csv
file1 = csv.reader(open('test1.csv', 'rb'), delimiter=',')
file2 = csv.reader(open('test2.csv', 'rb'), delimiter=',')
#loop through each line and add to a list A
file1list = []
for rowfirst in file1:
#print row[0] #prints first element
file1list.append(rowfirst)
#loop through each line and add to a list B
file2list = []
for rowsecond in file2:
# print row[0] #prints first element
file2list.append(rowsecond)
# the new file
"""
basically what I am doing here is checking if the start of a row, i.e. the [0]th element of the first file (as it is the first column) matches the [0]th element of the second file.
e.g. alfred and alfred match, SO in my new third list, add alfred WITH HIS SCORE (or without) to the new list
OK actually upon typing this for you guys to read I realized my program won't work properly anyways
"""
# the new file
finallines = []
for row1 in file2list: #I think this may be troublesome because if there are more rows in the original file (stored in file1list) they will be left out :S correct me if I'm wrong.
if row1 not in file1list: #if the row name column in the second file does not match a row name column in the first file show error, will be formalized later
print "Error"
else:
finallines.append(row1) #add to the new final list
print finallines
Of the 4 or 5 different ways which I was SURE would work, I chose to present this one because I think it gets across my idea best.
Just some nudges in the right direction would be much appreciated. I think I am really close but a side of me is telling me I have made a GIGANTIC logical error.
Thankyou for reading to the end! haha ♥♥ If you need any further explanation of what I am actually asking, fire away.

New Topic/Question
Reply




MultiQuote




|