Re: [Tutor] write dictionary to file

2014-06-23 Thread Ian D
I will read this when I get a minute, but I must say thanks for the explanation. tutor@python really is the most helpful mailing list EVER! > Date: Tue, 24 Jun 2014 00:54:30 +1000 > From: st...@pearwood.info > To: tutor@python.org > Subject: Re: [Tutor] write dictionary to file

Re: [Tutor] write dictionary to file

2014-06-23 Thread Steven D'Aprano
On Mon, Jun 23, 2014 at 09:17:44AM +, Ian D wrote: > > 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 fiel

Re: [Tutor] write dictionary to file

2014-06-23 Thread Ian D
> To: tutor@python.org > Date: Mon, 23 Jun 2014 09:17:44 + > Subject: Re: [Tutor] write dictionary to file > > >>> >>> import csv >>> >>> csvfile= open('StudentListToSort.csv', newline='') >>> spamreader = csv.Dict

Re: [Tutor] write dictionary to file

2014-06-23 Thread Peter Otten
Ian, take a step back; the first step to solve a problem is to state it clearly. You want to write a dict to a file. (1) What would the dictionary look like? Give concrete example, e. g. {"foo": 42, "bar": 'that\'s not "funny"'} (2) What should the resulting file look like when you open it in a

Re: [Tutor] write dictionary to file

2014-06-23 Thread Ian D
>> >> 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 l

Re: [Tutor] write dictionary to file

2014-06-23 Thread Ian D
Thanks a lot this is really helpful as have been the other posts. > > Have you tried reading the documentation? It sounds like you're just > throwing random bits of code at it and hoping something works. > > A better approach is to slow down and try to understand what the csv is > doing, what it

Re: [Tutor] write dictionary to file

2014-06-20 Thread Steven D'Aprano
On Fri, Jun 20, 2014 at 08:38:52AM +, Ian D wrote: > This is driving me nuts. > > I have tried many different things, but I just do not understand this > csv library. Have you tried reading the documentation? It sounds like you're just throwing random bits of code at it and hoping something

Re: [Tutor] write dictionary to file

2014-06-20 Thread Walter Prins
Hi, You've had a very good reply from Mark already however I want to add to it and further clarify what he pointed out (why exactly *are* you getting the tuple error after all?), also I've updated the prior example to help explain, see below: On 20 June 2014 15:11, Ian D wrote: > > import csv >

Re: [Tutor] write dictionary to file

2014-06-20 Thread Mark Lawrence
On 20/06/2014 15:11, Ian D wrote: Ok making some progress by changing the 'wb' to 'w' err no. unstuck again. import csv csvfile= open('StudentListToSort.csv', newline='') spamreader = csv.reader(csvfile,delimiter=',',quotechar='|') outfile = open('outfile.csv','w') for row in spamreader

Re: [Tutor] write dictionary to file

2014-06-20 Thread Ian D
> > Ok making some progress by changing the 'wb' to 'w' > err no. unstuck again. import csv csvfile= open('StudentListToSort.csv', newline='') spamreader = csv.reader(csvfile,delimiter=',',quotechar='|') outfile = open('outfile.csv','w') for row in spamreader: if row[4] == '6':

Re: [Tutor] write dictionary to file

2014-06-20 Thread Ian D
Ok making some progress by changing the 'wb' to 'w' > > Ok I see this error and the example shows a different type of syntax. > > > Rather than a file open for writing: > > outfile = open('output.csv', 'wb') > > > it uses > > with open('data.csv', 'w', newline='') as out: > > > > now is this writ

Re: [Tutor] write dictionary to file

2014-06-20 Thread Ian D
Thanks > > Nonetheless, having re-read your question and having googled a bit, it > seems that your problem might be related to Python 2 vs. Python 3, see > here: > http://stackoverflow.com/questions/24294457/python-typeerror-str-does-not-support-the-buffer-interface > > In short: In Python 2 you

Re: [Tutor] write dictionary to file

2014-06-20 Thread Walter Prins
Hi, Firstly an apology -- I only just noticed your original code was Python 3 -- my example was Python 2, so there would be some changes required to make the example work on Python 3... On 20 June 2014 11:19, Ian D wrote: > Thanks for your help > > > I am not much closer in understanding this so

Re: [Tutor] write dictionary to file

2014-06-20 Thread Ian D
Thanks for your help I am not much closer in understanding this so I am going to try and start with a simpler example for myself. I will try and write some values to a file as I am struggling even doing this. TypeError: 'str' does not support the buffer interface TypeError: 'tuple' does not

Re: [Tutor] write dictionary to file

2014-06-20 Thread Walter Prins
Hi, On 20 June 2014 09:38, Ian D wrote: > #so far this should read a file > #using dictreader and take a column and join some text onto it > > import csv > > csvfile= open('StudentListToSort.csv', newline='') > spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') > > #open a file to w

Re: [Tutor] write dictionary to file

2014-06-20 Thread Ian D
This is driving me nuts. I have tried many different things, but I just do not understand this csv library. I have tried passing various parameters to the writerow method and I am really getting nowhere fast. I just want to read from a file, join text to column and write to file. The wr

Re: [Tutor] write dictionary to file

2014-06-19 Thread Danny Yoo
On Thu, Jun 19, 2014 at 8:13 AM, Ian D wrote: > When trying to write my dictionary to a file I get: > > > f.write(output) > TypeError: 'tuple' does not support the buffer interface When writing structured data to disk files, you need to do something extra, because what disk files support are the

[Tutor] write dictionary to file

2014-06-19 Thread Ian D
When trying to write my dictionary to a file I get: f.write(output) TypeError: 'tuple' does not support the buffer interface using this example: #so far this should read a file #using dictreader and take a column and join some text onto it import csv csvfile= open('StudentListToSort.csv