
import csv
import os  
import os.path  
  
# this folder is custom  
rootdir="F:/cc/beijing_20150101-20151231/dir/"
readDir=[]
for parent,dirnames,filenames in os.walk(rootdir):  
    #case 1:  
    #for dirname in dirnames:  
        #print("parent folder is:" + parent)  
        #print("dirname is:" + dirname)  
    #case 2  
    for filename in filenames:    
        #print("parent folder is:" + parent)
        #print(filename)
        readDir.append(filename)
        #print (filename)
        #print("filename with full path:"+ os.path.join(parent,filename)) 
print(len(readDir))
b=[]
district={}
dirFile=['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','a11','a12','a13','a14','a15','a16','a17','a18','a19','a20','a21','a22','a23','a24','a25','a26','a27','a28','a29','a30','a31','a32','a33','a34','a35']

file1=open("a1.csv","w",newline="")
file2=open("a2.csv","w",newline="")
file3=open("a3.csv","w",newline="")
file4=open("a4.csv","w",newline="")
file5=open("a5.csv","w",newline="")
file6=open("a6.csv","w",newline="")
file7=open("a7.csv","w",newline="")
file8=open("a8.csv","w",newline="")
file9=open("a9.csv","w",newline="")
file10=open("a10.csv","w",newline="")
file11=open("a11.csv","w",newline="")
file12=open("a12.csv","w",newline="")
file13=open("a13.csv","w",newline="")
file14=open("a14.csv","w",newline="")
file15=open("a15.csv","w",newline="")
file16=open("a16.csv","w",newline="")
file17=open("a17.csv","w",newline="")
file18=open("a18.csv","w",newline="")
file19=open("a19.csv","w",newline="")
file20=open("a20.csv","w",newline="")
file21=open("a21.csv","w",newline="")
file22=open("a22.csv","w",newline="")
file23=open("a23.csv","w",newline="")
file24=open("a24.csv","w",newline="")
file25=open("a25.csv","w",newline="")
file26=open("a26.csv","w",newline="")
file27=open("a27.csv","w",newline="")
file28=open("a28.csv","w",newline="")
file29=open("a29.csv","w",newline="")
file30=open("a30.csv","w",newline="")
file31=open("a31.csv","w",newline="")
file32=open("a32.csv","w",newline="")
file33=open("a33.csv","w",newline="")
file34=open("a34.csv","w",newline="")
file35=open("a35.csv","w",newline="")
for filename in readDir:
    with open(filename, newline='',encoding='utf-8') as csvfile:
            
            
            csvwriter1 = csv.writer(file1,dialect = ("excel"))
            csvwriter2 = csv.writer(file2,dialect = ("excel"))
            csvwriter3 = csv.writer(file3,dialect = ("excel"))
            csvwriter4 = csv.writer(file4,dialect = ("excel"))
            csvwriter5 = csv.writer(file5,dialect = ("excel"))
            csvwriter6 = csv.writer(file6,dialect = ("excel"))
            csvwriter7 = csv.writer(file7,dialect = ("excel"))
            csvwriter8 = csv.writer(file8,dialect = ("excel"))
            csvwriter9 = csv.writer(file9,dialect = ("excel"))
            csvwriter10 = csv.writer(file10,dialect = ("excel"))
            csvwriter11 = csv.writer(file11,dialect = ("excel"))
            csvwriter12 = csv.writer(file12,dialect = ("excel"))
            csvwriter13 = csv.writer(file13,dialect = ("excel"))
            csvwriter14 = csv.writer(file14,dialect = ("excel"))
            csvwriter15 = csv.writer(file15,dialect = ("excel"))
            csvwriter16 = csv.writer(file16,dialect = ("excel"))
            csvwriter17 = csv.writer(file17,dialect = ("excel"))
            csvwriter18 = csv.writer(file18,dialect = ("excel"))
            csvwriter19 = csv.writer(file19,dialect = ("excel"))
            csvwriter20 = csv.writer(file20,dialect = ("excel"))
            csvwriter21 = csv.writer(file21,dialect = ("excel"))
            csvwriter22 = csv.writer(file22,dialect = ("excel"))
            csvwriter23 = csv.writer(file23,dialect = ("excel"))
            csvwriter24 = csv.writer(file24,dialect = ("excel"))
            csvwriter25 = csv.writer(file25,dialect = ("excel"))
            csvwriter26 = csv.writer(file26,dialect = ("excel"))
            csvwriter27 = csv.writer(file27,dialect = ("excel"))
            csvwriter28 = csv.writer(file28,dialect = ("excel"))
            csvwriter29 = csv.writer(file29,dialect = ("excel"))
            csvwriter30 = csv.writer(file30,dialect = ("excel"))
            csvwriter31 = csv.writer(file31,dialect = ("excel"))
            csvwriter32 = csv.writer(file32,dialect = ("excel"))
            csvwriter33 = csv.writer(file33,dialect = ("excel"))
            csvwriter34 = csv.writer(file34,dialect = ("excel"))
            csvwriter35 = csv.writer(file35,dialect = ("excel"))
            spamreader = csv.reader(csvfile, delimiter=',', quotechar=',')
           
            
            for row in spamreader:
                b=', '.join(row).split(",")
                
               
                if (b[2].strip()=="PM2.5")&(len(b)==38):
                
                  csvwriter1.writerow(['东单',(b[0]+b[1]),b[0],b[1],b[3]])
                  csvwriter2.writerow(['天坛',b[0]+b[1],b[0],b[1],b[4]])
                  csvwriter3.writerow(['官园',b[0]+b[1],b[0],b[1],b[5]])
                  csvwriter4.writerow(['万寿西宫',b[0]+b[1],b[0],b[1],b[6]])
                  csvwriter5.writerow(['奥体中心',b[0]+b[1],b[0],b[1],b[7]])
                  csvwriter6.writerow(['农展馆',b[0]+b[1],b[0],b[1],b[8]])  
                  csvwriter7.writerow(['万柳',b[0]+b[1],b[0],b[1],b[9]])
                  csvwriter8.writerow(['北部新区',b[0]+b[1],b[0],b[1],b[10]])
                  csvwriter9.writerow(['植物园',b[0]+b[1],b[0],b[1],b[11]])
                  csvwriter10.writerow(['丰台花园',b[0]+b[1],b[0],b[1],b[12]])
                  csvwriter11.writerow(['云岗',b[0]+b[1],b[0],b[1],b[13]])  
                  csvwriter12.writerow(['古城',b[0]+b[1],b[0],b[1],b[14]])
                  csvwriter13.writerow(['房山',b[0]+b[1],b[0],b[1],b[15]])
                  csvwriter14.writerow(['大兴',b[0]+b[1],b[0],b[1],b[16]])
                  csvwriter15.writerow(['亦庄',b[0]+b[1],b[0],b[1],b[17]])
                  csvwriter16.writerow(['通州',b[0]+b[1],b[0],b[1],b[18]])  
                  csvwriter17.writerow(['顺义',b[0]+b[1],b[0],b[1],b[19]])
                  csvwriter18.writerow(['昌平',b[0]+b[1],b[0],b[1],b[20]])
                  csvwriter19.writerow(['门头沟',b[0]+b[1],b[0],b[1],b[21]])
                  csvwriter20.writerow(['平谷',b[0]+b[1],b[0],b[1],b[22]])
                  csvwriter21.writerow(['怀柔',b[0]+b[1],b[0],b[1],b[23]])  
                  csvwriter22.writerow(['密云',b[0]+b[1],b[0],b[1],b[24]])
                  csvwriter23.writerow(['延庆',b[0]+b[1],b[0],b[1],b[25]])
                  csvwriter24.writerow(['定陵',b[0]+b[1],b[0],b[1],b[26]])
                  csvwriter25.writerow(['八达岭',b[0]+b[1],b[0],b[1],b[27]])
                  csvwriter26.writerow(['密云水库',b[0]+b[1],b[0],b[1],b[28]])  
                  csvwriter27.writerow(['东高村',b[0]+b[1],b[0],b[1],b[29]])
                  csvwriter28.writerow(['永乐店',b[0]+b[1],b[0],b[1],b[30]])
                  csvwriter29.writerow(['榆垡',b[0]+b[1],b[0],b[1],b[31]])
                  csvwriter30.writerow(['琉璃河',b[0]+b[1],b[0],b[1],b[32]])
                  csvwriter31.writerow(['前门',b[0]+b[1],b[0],b[1],b[33]])  
                  csvwriter32.writerow(['永定门内',b[0]+b[1],b[0],b[1],b[34]])
                  csvwriter33.writerow(['西直门北',b[0]+b[1],b[0],b[1],b[35]])
                  csvwriter34.writerow(['南三环',b[0]+b[1],b[0],b[1],b[36]])
                  csvwriter35.writerow(['东四环',b[0]+b[1],b[0],b[1],b[37]])
file1.close()
file2.close()
file3.close()
file4.close()
file5.close()
file6.close()
file7.close()
file8.close()
file9.close()
file10.close()
file11.close()
file12.close()
file13.close()
file14.close()
file15.close()
file16.close()
file17.close()
file18.close()
file19.close()
file20.close()
file21.close()
file22.close()
file23.close()
file24.close()
file25.close()
file26.close()
file27.close()
file28.close()
file29.close()
file30.close()
file31.close()
file32.close()
file33.close()
file34.close()
file35.close()
