Last active
August 29, 2015 14:19
-
-
Save anjesh/1814a4512171a6bc4289 to your computer and use it in GitHub Desktop.
Converts the count in the column of csv into multiple rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
# convert the count in the column into multiple rows | |
# e.g. | |
# a,b,c,2,1 | |
# becomes | |
# a,b,c,Male | |
# a,b,c,Male | |
# a,b,c,FeMale | |
import csv | |
csvoutfile = "out.csv" | |
csvinfile = "in.csv" | |
infile = open(csvinfile, 'r') | |
outfile = open(csvoutfile, 'w') | |
reader = csv.reader(infile) | |
csvwriter = csv.writer(outfile, delimiter=',', quotechar='"') | |
for row in reader: | |
year = row[0] | |
district = row[1] | |
cause = row[2] | |
malecount = row[3] | |
femalecount = row[4] | |
try: | |
if malecount and int(malecount) and malecount > 0: | |
for i in range(0,int(malecount)): | |
csvwriter.writerow([year, district, cause, "Male"]) | |
except: | |
#ignore any other string than number in the malecount column | |
pass | |
try: | |
if femalecount and int(femalecount) and femalecount > 0: | |
for i in range(0,int(femalecount)): | |
csvwriter.writerow([year, district, cause, "Female"]) | |
except: | |
pass |
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 5 in line 1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# csv infile example | |
आ.व.,जिल्ला,अपराध शिर्षक,पुरुष,महिला | |
066/67,कञ्चनपुर,विष सेवन गरी मृत्यू,36,27 | |
066/67,कञ्चनपुर,झुण्डी मानिसको मृत्यु,18,28 | |
066/67,कञ्चनपुर,पानीमा हाम फाली,Female,1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment