python按条件读取内容

循环读取多个文件的并输出多个文件的:只实现到读取一个文件输出一个文件,因为数据量太大了

import pandas as pd
import os
import glob
import codecs
import os
import glob
import codecs
import csv
from collections import Counter
  

file_name = []
data=[]
data1=[]
dd=[]
dd1=[]

keys=['countUnormal','countNormal']
 
#path = 'F:/zwork/data/统计结果/t1k'
path = 'D:\\Desktop'
csv_files = glob.glob(os.path.join(path, "*.csv"))

#f='F:/zwork/data/1k.csv'
for f in csv_files:
    path=f.split("\\")[-1]
    file_name.append(path)
    with open(f,encoding='utf-8') as csv_file:
        writer_obj = csv.DictReader(csv_file)
        
        countNormal = 0
        countUnormal = 0
        for row in writer_obj:
    
            if(row['rec_code']!='14')&(row['severity']=='3'):

文件内容的限制条件
                countUnormal  = countUnormal  +1
            else:
               countNormal = countNormal + 1
    
        data.append([countUnormal, countNormal])
        print(data)
name2=[file_name]
test=pd.DataFrame(columns=keys,index=name2,data=data)

columns代表是列名,index代表的是文件名
test.to_csv("F:/zwork/testcsv/4.csv",encoding="gbk")


 

你可能感兴趣的:(每日学习,python)