Howw to prevent the duplication of any value in a column within a CSV file (python)

Adam Davis peakgraphicz at gmail.com
Wed Apr 27 02:26:19 EDT 2016


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
> >> <Joaquin.Alzola at lebara.com> 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?



More information about the Python-list mailing list