Ok I used :
spamwriter.writerow({'user':email,'first':row['first'],'last':row['last'], 'password':row['password'] }) Seems to be almost finished thanks ---------------------------------------- > From: dux...@hotmail.com > To: tutor@python.org > Date: Mon, 23 Jun 2014 09:17:44 +0000 > Subject: Re: [Tutor] write dictionary to file > > >>> >>> import csv >>> >>> csvfile= open('StudentListToSort.csv', newline='') >>> spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') >> >> Are you sure that your input file uses | as a quote character and , as >> the field delimiter? > > > No I overlooked this > > >> >> >>> #open a file to write to later >>> fields = ['user','first','last','password','year'] >>> csvoutput = open('output.csv', 'wb+') >> >> I'm pretty sure you don't want to use "wb+" mode. Since you're using >> Python 3, I think you should just use "w" mode. >> >> The "b" turns on binary mode, and in Python 3 you don't want that. The >> "+" turns on either "read/write" mode or "append" mode, I don't remember >> which, but either way I don't think it's necessary for what you are >> doing. > > > Yes I came across this python 3 idiosyncrasy > > > >> >> >>> spamwriter = csv.DictWriter(csvoutput,fieldnames=fields, delimiter=' ') >> >> Now you're turning every , delimiter into a space. Are you sure you want >> that? >> >> > > > Overlooked this > > > >>> for row in spamreader: >>> if row['year'] == '40': >>> username = row['user'] >>> email = "".join([username,'@email.com]) >> >> Syntax error: you left out the closing single quote. You need: >> >> email = "".join([username,'@email.com']) >> > > > I think I may have messed that up editing code for public viewing. > > > > > On to your example..... > > > >> >> import csv >> >> # Open the file we're reading from. >> csvfile= open('StudentListToSort.csv', newline='') >> # Open a file to write to. >> csvoutput = open('output.csv', 'w', newline='') >> >> fields = ['user', 'first', 'last', 'password', 'year'] >> >> # Are you sure you want | as the quote character? >> spamreader = csv.DictReader(csvfile, delimiter=',', quotechar='|') > >> >> >> # Still using , as a delimiter, not space. >> spamwriter = csv.DictWriter(csvoutput, fieldnames=fields, delimiter=',') >> >> for row in spamreader: >> if row['year'] == '40': >> email = row['user'] + '@email.com' >> output = [ row[fieldname] for fieldname in fields ] > > > I am unsure about this syntax [ row[fieldname] for fieldname in fields ] > > > > The FOR loop is not in any context I have used before. I have seen > examples(recently) so its obviously standard practice, but not something I > would ever think to type. > > > >> print(output) >> # DictWriter needs a dict, not a list. >> spamwriter.writerow({name: row[name] for name in fields}) >> print("Warning: email calculated but never used:", email) > > > > > And this writerow syntax is something new for me, as are dictionaries( which > I have tried to read up and understand.) > > > >>spamwriter.writerow({name: row[name] for name in fields}) > > > > This looks like the same loop as the one above but a dictionary using curly > braces(for dict), its the same unfamiliar way of writing a FOR loop for me. > > > > > So if I wanted multiple rows in the output csv file I would try: > > > ({name: row[name], row[email, row[first] for name in fields}) > > > which doesn't work as the syntax is invalid, so what would I need to change > to allow spamwriter.writerow to generate multiple fields? > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor