Re: Howw to prevent the duplication of any value in a column within a CSV file (python)
On Tuesday, 26 April 2016 17:14:36 UTC+1, Ian wrote: > On Tue, Apr 26, 2016 at 10:01 AM, wrote: > > I am wondering how to make my code function so it does not allow any of the > > same values to be entered into a column in my CSV file created through > > python. So I need to read into the CSV file and check if any names have > > already been entered. If they have, the user must not be allowed to enter > > this value. > > Assuming the file is reasonably small enough to fit into memory, > create a set and populate it with the values in the column. When the > user enters a new value, check whether it's already in the set. That's exactly what I want to do, however, I'm unsure how to check whether it's in the set? -- https://mail.python.org/mailman/listinfo/python-list
Re: Howw to prevent the duplication of any value in a column within a CSV file (python)
On Tuesday, 26 April 2016 17:09:10 UTC+1, Joel Goldstick wrote:
> On Tue, Apr 26, 2016 at 12:01 PM, wrote:
> > I am wondering how to make my code function so it does not allow any of the
> > same values to be entered into a column in my CSV file created through
> > python. So I need to read into the CSV file and check if any names have
> > already been entered. If they have, the user must not be allowed to enter
> > this value.
> >
> > Thanks in advance!
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> Show your code and a few lines of your file with useful data. Show
> the result you want
>
> --
> Joel Goldstick
> http://joelgoldstick.com/blog
> http://cc-baseballstats.info/stats/birthdays
Here's my section of code:
with open(class_code+".csv", 'a') as csvfile:
fieldnames = ["Name", "Score 1", "Score 2", "Score 3", "Average"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
#writer.writeheader()
writer.writerow({"Name": name, "Score 1": score1, "Score 2": score2,
"Score 3": score3})
It prints when requested as:
Adam,1,0,0,0.
Jake,9,10,6,8.334
Tom,5,3,3,3.6665
So basically, if anyone called Adam, Jake or Tom tries the quiz again, it will
not let them.
Thanks
--
https://mail.python.org/mailman/listinfo/python-list
Re: Howw to prevent the duplication of any value in a column within a CSV file (python)
On Tuesday, 26 April 2016 17:01:41 UTC+1, Adam Davis wrote:
> I am wondering how to make my code function so it does not allow any of the
> same values to be entered into a column in my CSV file created through
> python. So I need to read into the CSV file and check if any names have
> already been entered. If they have, the user must not be allowed to enter
> this value.
>
> Thanks in advance!
Here's my section of code:
with open(class_code+".csv", 'a') as csvfile:
fieldnames = ["Name", "Score 1", "Score 2", "Score 3", "Average"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
#writer.writeheader()
writer.writerow({"Name": name, "Score 1": score1, "Score 2": score2,
"Score 3": score3})
It prints when requested as:
Adam,1,0,0,0.
Jake,9,10,6,8.334
Tom,5,3,3,3.6665
So basically, if anyone called Adam, Jake or Tom tries the quiz again, it will
not let them.
Thanks
--
https://mail.python.org/mailman/listinfo/python-list
Re: Howw to prevent the duplication of any value in a column within a CSV file (python)
On Tuesday, 26 April 2016 20:52:54 UTC+1, Ian wrote:
> On Tue, Apr 26, 2016 at 1:05 PM, Joaquin Alzola
> wrote:
> > Just an example. Didn't use the csv but just hope that it helps.
> >
> > name=[]
> > name_exist="Dop"
> >
> > with open("dop.csv") as f:
> > for line in f:
> > line_split=line.split(',')
> > name.append(line_strip[0])
> > if name_exist in name:
> > print "found name " + name_exist + " Can not append"
> > else:
> > file = open("dop.csv",'a')
> > print "No name found"
> > file.write(name_exist)
> > file.close()
>
> Except that as noted elsewhere in the thread a set would be a better
> choice than a list, since checking membership is much faster on a set.
Thanks for your help Ian. I'm a beginner right now, could you instruct me as to
how I would create a set?
--
https://mail.python.org/mailman/listinfo/python-list
Re: Howw to prevent the duplication of any value in a column within a CSV file (python)
On Tuesday, 26 April 2016 21:23:58 UTC+1, MRAB wrote:
> On 2016-04-26 21:07, Adam Davis wrote:
> > On Tuesday, 26 April 2016 20:52:54 UTC+1, Ian wrote:
> >> On Tue, Apr 26, 2016 at 1:05 PM, Joaquin Alzola
> >> wrote:
> >> > Just an example. Didn't use the csv but just hope that it helps.
> >> >
> >> > name=[]
> >> > name_exist="Dop"
> >> >
> >> > with open("dop.csv") as f:
> >> > for line in f:
> >> > line_split=line.split(',')
> >> > name.append(line_strip[0])
> >> > if name_exist in name:
> >> > print "found name " + name_exist + " Can not append"
> >> > else:
> >> > file = open("dop.csv",'a')
> >> > print "No name found"
> >> > file.write(name_exist)
> >> > file.close()
> >>
> >> Except that as noted elsewhere in the thread a set would be a better
> >> choice than a list, since checking membership is much faster on a set.
> >
> > Thanks for your help Ian. I'm a beginner right now, could you instruct me
> > as to how I would create a set?
> >
> Create a set:
>
> the_set = set()
>
> Add an item to a set:
>
> the_set.add(item)
I understand what you're saying! But where you say: " the_set = set()", what
would go within the set brackets?
--
https://mail.python.org/mailman/listinfo/python-list
Re: Howw to prevent the duplication of any value in a column within a CSV file (python)
On Wednesday, 27 April 2016 07:37:42 UTC+1, Chris Angelico wrote: > On Wed, Apr 27, 2016 at 4:26 PM, Adam Davis wrote: > > I understand what you're saying! But where you say: " the_set = set()", > > what would go within the set brackets? > > Nothing. The empty parentheses mean "call this with no arguments", and > when you call the set constructor like that, you get back an empty > set. > > ChrisA Thanks Chris. Where Ian says 'the_set.add(item)', what would be put within item in terms of my codes function/aim? -- https://mail.python.org/mailman/listinfo/python-list
