# -*- coding: cp936 -*-
import os
import re
import csv
#from __future__ import division
ret = []
new = []
datas =list()
def getListFiles(path): 
  assert os.path.isdir(path), '%s not exist.' % path 
  for root, dirs, files in os.walk(path):
    #print '%s' % (files)
    #print '%s, %s' % (root, dirs) 
    #print '%s, %s, %s' % (root, dirs, files) 
    for filespath in files: 
      #ret.append(os.path.join(root,filespath))
      if filespath.find(".xsd_RE.txt")!=-1:#按照文件类型过滤
        ret.append(filespath)
        #new.append(os.path.splitext(filespath)[0]+'.dtd')
  return ret
def writeFile(content,filename):
    myfile = open(filename,'a')  
    myfile.write(content)
    myfile.write('\r\n')
    myfile.close()
num = len(getListFiles('.'))
print num
i=2
for m in range(num):
  #print ret[m]
  ciku=open(ret[m],'r')
  cikus=ciku.readlines()
  #write=open()
  numline=-1
  sum1=0
  for line in cikus:
    list1=re.split('\||,',line)
    sum1 =sum1+len(list1)
    numline= numline+1
    #texts=re.findall("(?<=[(])[^()]+\.[^()]+(?=[)])",line)
    #new=str(i)+'.txt'
    #writeFile(text,'aa.txt')
    #if line.find("EMPTY")!=-1:
      #line="EMPTY"
    #elif line.find("ANY")!=-1:
      #line ="ANY"
    #else:
      #index = line.find(" (")
      #line = line[index+1:]
    #for text in texts:
      #print text
  if numline!=0:
    sum1=sum1*1.0
    numline=numline*1.0
    res=sum1/numline
    datas.append([ret[m][1:],str(res)])
    #writeFile(ret[m][1:]+":"+str(res),'res.txt')
  else:
    datas.append([ret[m][1:],"0"])
    #writeFile(ret[m][1:]+":"+"0",'res.txt')
  i=i+1
  print i
csvfile = file('xsd.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['XSD', 'density'])
writer.writerows(datas) 
csvfile.close()


  
