-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathcomm_utils.py
More file actions
186 lines (147 loc) · 4.06 KB
/
comm_utils.py
File metadata and controls
186 lines (147 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
此类为测试用例的通用工具类
"""
import os
import re
import pandas as pd
import xml.etree.ElementTree as ET
def file_exist(file_path):
"""
判断文件是否存在
Args:
file_path: 文件路径 str
"""
return os.path.exists(file_path)
def is_chinese_chars_regex(s):
"""
字符串是否是全中文
Args:
s: 字符串 str
"""
pattern = re.compile(r'[\u4e00-\u9fff]')
matches = pattern.findall(s)
return len(matches) > 1
def delete_file(file_path):
"""
删除文件
Args:
file_path: 需要删除的文件路径 str
"""
os.remove(file_path)
def get_file_context(file_path):
"""
获取文件中的内容
Args:
file_path: 文件路径 str
"""
res = ""
with open(file_path, 'r', encoding='utf-8') as file:
res = file.read()
return res
def touch_file(file_path):
"""
创建文件
Args:
file_path: 文件路径 str
"""
with open(file_path, 'w'):
pass
def get_all_name_values(file_path):
"""
获取所有xml中name的值
Args:
file_path: 文件路径 str
"""
xml_data = get_file_context(file_path)
root = ET.fromstring(xml_data)
res_list = []
for obj in root.findall('object'):
res_list.append(obj.find('name').text)
return res_list
def get_file_by_suffix(dir_path, suffix):
"""
获取目录下固定后缀的文件名
Args:
dir_path: 目录名称 str
suffix: 后缀名称 str
"""
xlsx_files = []
file_names = os.listdir(dir_path)
for file_name in file_names:
if file_name.endswith(suffix):
xlsx_files.append(file_name)
return xlsx_files
def get_colum_content(file_path, column_index, sheet_name='Sheet1'):
"""
获取excel中
Args:
file_path: 文件路径 str
column_index: 需要查询标题的列索引,从0开始 int
sheet_name: sheet名称 str
"""
df = pd.read_excel(file_path, sheet_name=sheet_name)
headers = df.columns.tolist()
return headers[column_index]
def get_content(file_path, row_index, col_index, sheet_name='Sheet1'):
"""
获取excel文件具体行列的值
Args:
file_path: 文件路径 str
row_index: 需要查询的行索引,从0开始 int
col_index: 需要查询的列索引,从0开始 int
sheet_name: sheet名称 str
"""
df = pd.read_excel(file_path, sheet_name)
first_value = df.iloc[row_index, col_index]
return first_value
def get_file_by_suffix(dir_path, suffix):
"""
获取目录下固定后缀的文件名
Args:
dir_path: 目录名称 str
suffix: 后缀名称 str
"""
xlsx_files = []
file_names = os.listdir(dir_path)
for file_name in file_names:
if file_name.endswith(suffix):
xlsx_files.append(file_name)
return xlsx_files
def get_all_sheet_names(file_path):
"""
获取excel文件的所有sheet名称
Args:
file_path: 文件路径 str
"""
excel_file = pd.ExcelFile(file_path)
sheet_names = excel_file.sheet_names
return sheet_names
def get_filter_names(file_path, column_index):
"""
获取excel文件中某列的所有值分类
Args:
file_path: 文件路径 str
column_index: 需要筛选的列索引,从0开始 int
"""
df = pd.read_excel(file_path)
column_name = df.columns[column_index]
# 除去残留nan值
df = df.dropna(subset=[column_name])
categories = df[column_name].unique().tolist()
return categories
def get_latest_file(dir_path):
"""
获取目录下最新生成的文件路径
Args:
dir_path: 目录路径 str
"""
latest_file = None
latest_ctime = 0
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
ctime = os.path.getctime(file_path)
if ctime > latest_ctime:
latest_ctime = ctime
latest_file = file_path
return latest_file