python - 我编写了一个 python 代码来检查空值并且代码工作正常,但我需要简化代码
问题描述
我想检查有很多子文件夹的 excel 文件中的空值。我编写了一个 python 代码来检查每个文件夹中 excel 文件中的空值,并且代码工作正常,但我需要简化代码。
import os ,uuidtre
import numpy as np
import pandas as pd
import logging
path =r"C:\Users\Documents\python\Emp"
files = os.listdir(path)
for filename in files:
pathchild=path+'\\'+filename
file = os.listdir(pathchild)
files_xls = [f for f in file if f[-4:]== 'xlsx' ]
child_folders= [f for f in file if f[-4:]!= 'xlsx']
for f1 in files_xls:
filepath=pathchild+'\\'+f1
df = pd.read_excel(filepath, engine='openpyxl')
count_nan = df.isnull().sum().sum()
logging.basicConfig(filename='nanTest.log', level=logging.INFO,format='%(message)s')
if count_nan ==0:
none=' No none value in the excel file'
else:
none=' There is none values founded in the excel file'
output='File name: '+ f1, 'File path: '+ filepath, none
logging.info(output)
for f2 in child_folders:
patha=pathchild+'\\'+f2
file1 = os.listdir(patha)
files_xls1 = [f for f in file1 if f[-4:]== 'xlsx']
for f3 in files_xls1:
filechildpath=patha+'\\'+f3
df = pd.read_excel(filechildpath, engine='openpyxl')
count_nan = df.isnull().sum().sum()
if count_nan ==0:
none=' No none value in the excel file'
else:
none=' There is none values founded in the excel file'
output='File name: '+ f3,'File path: '+ filepath, none
logging.info(output)
解决方案
推荐阅读
- javascript - React Native 如何同时获取多个 API?
- angular - 角度自定义样式指令值在开头未定义
- python - Saleforce 从两个不同的对象中检索字段 - (SOQL) Simple Salesforce Python
- c# - Azure.Search.Documents 在将 CORS 添加到索引时抛出错误(以编程方式)
- html - html表中行的条件样式
- postgresql - 为什么 postgres 查询无法识别 @Query 字符串中的字符串类型?
- file - C++ 数据资源文件
- flutter - 如何在flutter Streambuilder中重新启动流?
- node.js - 无法使用 FormData 从表单中获取数据
- c++ - 与基类测试相比,如何使派生类测试在 INSTANTIATE_TEST_CASE_P 中使用 ::testing::Combine 的更多值参数?