|
@@ -3,9 +3,9 @@
|
|
|
# @Author : 魏志亮
|
|
|
import ast
|
|
|
import datetime
|
|
|
-import os
|
|
|
import shutil
|
|
|
import warnings
|
|
|
+from os import *
|
|
|
|
|
|
import chardet
|
|
|
import pandas as pd
|
|
@@ -89,7 +89,7 @@ def read_file_to_df(file_path, read_cols=list(), trans_cols=None, nrows=None, no
|
|
|
find_cols = list()
|
|
|
if trans_cols:
|
|
|
header = find_read_header(file_path, trans_cols, resolve_col_prefix)
|
|
|
- trans_print(os.path.basename(file_path), "读取第", header, "行")
|
|
|
+ trans_print(path.basename(file_path), "读取第", header, "行")
|
|
|
if header is None:
|
|
|
if not_find_header == 'raise':
|
|
|
message = '未匹配到开始行,请检查并重新指定'
|
|
@@ -137,7 +137,7 @@ def read_file_to_df(file_path, read_cols=list(), trans_cols=None, nrows=None, no
|
|
|
trans_print('文件读取成功:', file_path, '数据数量:', df.shape, '耗时:', datetime.datetime.now() - begin)
|
|
|
except Exception as e:
|
|
|
trans_print('读取文件出错', file_path, str(e))
|
|
|
- message = '文件:' + os.path.basename(file_path) + ',' + str(e)
|
|
|
+ message = '文件:' + path.basename(file_path) + ',' + str(e)
|
|
|
raise ValueError(message)
|
|
|
|
|
|
return df
|
|
@@ -145,11 +145,11 @@ def read_file_to_df(file_path, read_cols=list(), trans_cols=None, nrows=None, no
|
|
|
|
|
|
def __build_directory_dict(directory_dict, path, filter_types=None):
|
|
|
# 遍历目录下的所有项
|
|
|
- for item in os.listdir(path):
|
|
|
- item_path = os.path.join(path, item)
|
|
|
- if os.path.isdir(item_path):
|
|
|
+ for item in listdir(path):
|
|
|
+ item_path = path.join(path, item)
|
|
|
+ if path.isdir(item_path):
|
|
|
__build_directory_dict(directory_dict, item_path, filter_types=filter_types)
|
|
|
- elif os.path.isfile(item_path):
|
|
|
+ elif path.isfile(item_path):
|
|
|
if path not in directory_dict:
|
|
|
directory_dict[path] = []
|
|
|
|
|
@@ -164,7 +164,7 @@ def __build_directory_dict(directory_dict, path, filter_types=None):
|
|
|
def read_excel_files(read_path, filter_types=None):
|
|
|
if filter_types is None:
|
|
|
filter_types = ['xls', 'xlsx', 'csv', 'gz']
|
|
|
- if os.path.isfile(read_path):
|
|
|
+ if path.isfile(read_path):
|
|
|
return [read_path]
|
|
|
|
|
|
directory_dict = {}
|
|
@@ -177,7 +177,7 @@ def read_excel_files(read_path, filter_types=None):
|
|
|
def read_files(read_path, filter_types=None):
|
|
|
if filter_types is None:
|
|
|
filter_types = ['xls', 'xlsx', 'csv', 'gz', 'zip', 'rar']
|
|
|
- if os.path.isfile(read_path):
|
|
|
+ if path.isfile(read_path):
|
|
|
return [read_path]
|
|
|
directory_dict = {}
|
|
|
__build_directory_dict(directory_dict, read_path, filter_types=filter_types)
|
|
@@ -203,10 +203,10 @@ def create_file_path(path, is_file_path=False):
|
|
|
:param is_file_path: 传入的path是否包含具体的文件名
|
|
|
"""
|
|
|
if is_file_path:
|
|
|
- path = os.path.dirname(path)
|
|
|
+ path = path.dirname(path)
|
|
|
|
|
|
- if not os.path.exists(path):
|
|
|
- os.makedirs(path, exist_ok=True)
|
|
|
+ if not path.exists(path):
|
|
|
+ makedirs(path, exist_ok=True)
|
|
|
|
|
|
|
|
|
def valid_eval(eval_str):
|
|
@@ -228,9 +228,10 @@ if __name__ == '__main__':
|
|
|
# aa = valid_eval("df['123'].apply(lambda wind_name: wind_name.replace('元宝山','').replace('号风机',''))")
|
|
|
# print(aa)
|
|
|
#
|
|
|
- # aa = valid_eval("'记录时间' if column == '时间' else column;import os; os.path")
|
|
|
+ # aa = valid_eval("'记录时间' if column == '时间' else column;from os import *; path")
|
|
|
# print(aa)
|
|
|
|
|
|
- df = read_file_to_df(r"D:\data\11-12月.xls", trans_cols=['风机', '时间', '有功功率', '无功功率', '功率因数', '频率'], nrows=30)
|
|
|
+ df = read_file_to_df(r"D:\data\11-12月.xls",
|
|
|
+ trans_cols=['风机', '时间', '有功功率', '无功功率', '功率因数', '频率'], nrows=30)
|
|
|
|
|
|
print(df.columns)
|