python csv don't print duplicates -



python csv don't print duplicates -

we have many csv files follows:

name,type 1,fuji 2,fuji 3,fuji 4,fuji 5,washington 6,washington 7,washington 8,washington 9,washington

we print out types of apples without printing duplicates.

fuji:6 washington:4 gaza:1

or

fuji washington gaza

the next our attempt. although doesn't seem work unknown reasons.

# python 2.7 import csv import glob import collections collections import counter list = glob.glob('c:apple*.csv') file in list: infile = open(file, "rb") reader = csv.reader(infile) column in reader: discipline = column[1] print collections.counter(discipline) infile.close()

i haven't used csv module before here quick effort @ think might trying achieve.

import csv src = r'c:\apples_before.csv' dst = r'c:\apples_after.csv' apples = set([]) # read file. open(src, 'r') srcfile: reader = csv.reader(srcfile, delimiter=',') index, row in enumerate(reader): if index == 0: go on apples.add(row[1]) # write file. # @warning: please note making assumption in terms of number # component. assuming row number. open(dst, 'w') dstfile: author = csv.writer(dstfile, delimiter=',') index, apple in enumerate(apples): if index == 0: writer.writerow(['name', 'type']) writer.writerow([index + 1, apple])

python-2.7 csv duplicates

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -