I've got a function that adds new data to a csv file. I've got it somewhat working. However, I'm having a few problems.
- When I add the new values
(name, phone, address, birthday), it adds them all in one column, rather than separate columns in the same row. (Not really much idea on how to split them up in various columns...) - I can only add numbers rather than string values. So if I write
add_friend(blah, 31, 12, 45), it will come back sayingblahis not defined. However, if I writeadd_friend(3,4,5,6), it'll add that to the new row—but, into a single column - An objective with the function is: If you try and add a friend that's already in the csv (say, Bob), and his address, phone, birthday are already in the csv, if you
add_friend(Bob, address, phone, birthday), it should stateFalse, and not add it. However, I have no clue how to do this. Any ideas?
Here is my code:
def add_friend (name, phone, address, birthday):
with open('friends.csv', 'ab') as f:
newrow = [name, phone, address, birthday]
friendwriter = csv.writer(open('friends.csv', 'ab'), delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
friendwriter.writerow(newrow)
#friendreader = csv.reader(open('friends.csv', 'rb'), delimiter=' ', quotechar='|')
#for row in friendreader:
#print ' '.join(row)
print newrow