upplib 3.4.1__py3-none-any.whl → 3.4.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- upplib/datetime_function.py +14 -14
- upplib/file_to_text.py +222 -219
- {upplib-3.4.1.dist-info → upplib-3.4.3.dist-info}/METADATA +1 -1
- {upplib-3.4.1.dist-info → upplib-3.4.3.dist-info}/RECORD +7 -7
- {upplib-3.4.1.dist-info → upplib-3.4.3.dist-info}/WHEEL +0 -0
- {upplib-3.4.1.dist-info → upplib-3.4.3.dist-info}/licenses/LICENSE +0 -0
- {upplib-3.4.1.dist-info → upplib-3.4.3.dist-info}/top_level.txt +0 -0
upplib/datetime_function.py
CHANGED
|
@@ -37,14 +37,12 @@ def get_tz(tz_info: Union[str, timezone]) -> timezone:
|
|
|
37
37
|
raise ValueError(f"不支持的时区格式: {tz_info}")
|
|
38
38
|
|
|
39
39
|
|
|
40
|
-
def to_datetime(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
default_tz: Union[str, timezone] = None
|
|
47
|
-
) -> Union[datetime, str, None]:
|
|
40
|
+
def to_datetime(s: Any = None,
|
|
41
|
+
pattern: str = None,
|
|
42
|
+
r_str: bool = False,
|
|
43
|
+
error_is_none: bool = False,
|
|
44
|
+
tz: Optional[Union[str, timezone]] = None,
|
|
45
|
+
default_tz: Union[str, timezone] = None) -> Union[datetime, str, None]:
|
|
48
46
|
"""
|
|
49
47
|
将字符串或时间戳转换为 datetime 对象,支持时区处理。
|
|
50
48
|
error_is_none : 当发生错误的时候,是否返回 None
|
|
@@ -146,7 +144,10 @@ def to_datetime_str(s: Any = None,
|
|
|
146
144
|
default_tz: Union[str, timezone] = None) -> datetime | str:
|
|
147
145
|
"""
|
|
148
146
|
将 s 先转成 datetime, 然后再转成字符串
|
|
149
|
-
|
|
147
|
+
:type pattern: str
|
|
148
|
+
:type pattern_str: str
|
|
149
|
+
:type tz: str
|
|
150
|
+
:type default_tz: str
|
|
150
151
|
:param s: 输入值(可以是字符串或其他类型)
|
|
151
152
|
"""
|
|
152
153
|
r_s = to_datetime(s, pattern=pattern, tz=tz, r_str=False, default_tz=default_tz)
|
|
@@ -218,8 +219,7 @@ def get_timestamp(s: Any = None) -> int:
|
|
|
218
219
|
|
|
219
220
|
def get_datetime_number_str(s: Any = None,
|
|
220
221
|
length: int = None,
|
|
221
|
-
remove_dz: bool = True
|
|
222
|
-
) -> str:
|
|
222
|
+
remove_dz: bool = True) -> str:
|
|
223
223
|
"""获取 datetime , 然后转成字符串, 然后, 只保留数字
|
|
224
224
|
Args:
|
|
225
225
|
s: 输入值,可以是任何类型,如果是None则使用当前时间
|
|
@@ -230,11 +230,11 @@ def get_datetime_number_str(s: Any = None,
|
|
|
230
230
|
"""
|
|
231
231
|
s1 = to_datetime(s)
|
|
232
232
|
digits_only = re.sub(r'\D', '', str(s1))
|
|
233
|
-
# 如果指定了长度,取最后length位
|
|
234
|
-
if length is not None and length > 0:
|
|
235
|
-
return digits_only[-length:] if len(digits_only) > length else digits_only
|
|
236
233
|
if len(digits_only) > 4 and remove_dz:
|
|
237
234
|
digits_only = digits_only[:-4]
|
|
235
|
+
# 如果指定了长度,取最后length位
|
|
236
|
+
if length is not None and length > 0:
|
|
237
|
+
digits_only = digits_only[-length:] if len(digits_only) > length else digits_only
|
|
238
238
|
return digits_only
|
|
239
239
|
|
|
240
240
|
|
upplib/file_to_text.py
CHANGED
|
@@ -3,224 +3,6 @@ from upplib.common_package import *
|
|
|
3
3
|
from upplib.file_function import get_file
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
def to_list(file_name: str = 'a.txt',
|
|
7
|
-
sep: str = None,
|
|
8
|
-
sep_line: str = None,
|
|
9
|
-
sep_line_contain: str = None,
|
|
10
|
-
sep_line_prefix: str = None,
|
|
11
|
-
sep_line_suffix: str = None,
|
|
12
|
-
sep_all: str = None,
|
|
13
|
-
line_ignore_start_with: list[str] | set[str] | str = None,
|
|
14
|
-
line_ignore_end_with: list[str] | set[str] | str | None = None,
|
|
15
|
-
line_ignore_empty: bool | None = None,
|
|
16
|
-
start_index: int = None,
|
|
17
|
-
start_line: str = None,
|
|
18
|
-
end_index: int = None,
|
|
19
|
-
end_line: str = None,
|
|
20
|
-
count: int = None,
|
|
21
|
-
sheet_index: int = 1,
|
|
22
|
-
column_index: list[str] | set[str] | str | None = None,
|
|
23
|
-
column_date: list[str] | set[str] | str | None = None,
|
|
24
|
-
column_datetime: list[str] | set[str] | str | None = None) -> list:
|
|
25
|
-
"""
|
|
26
|
-
当读取 txt 之类的文件的时候
|
|
27
|
-
将 txt 文件读取到 list 中, 每一行自动过滤掉行前行后的特殊字符
|
|
28
|
-
sep : 是否对每一行进行分割,如果存在这个字段,就分割
|
|
29
|
-
sep_all : 将文件转化成一个字符串,然后对这个字符串,再次总体分割
|
|
30
|
-
start_index : 从这个地方开始读取,从1开始标号 , 包含这一行
|
|
31
|
-
start_line : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 包含这一行
|
|
32
|
-
end_index : 读取到这个地方结束,从1开始标号 , 不包含这一行
|
|
33
|
-
end_line : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行
|
|
34
|
-
count : 读取指定的行数
|
|
35
|
-
##############################################
|
|
36
|
-
当读取 excel 之类的文件的时候
|
|
37
|
-
将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
|
|
38
|
-
sheet : 从 1 开始编号,
|
|
39
|
-
column_index : 从 1 开始编号, 指定列
|
|
40
|
-
column_index : 如果是指定值, 这个时候返回的是一个 list, 没有嵌套 list
|
|
41
|
-
column_index : 如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
|
|
42
|
-
column_date : 指定日期格式的列,规则与 column_index 一样
|
|
43
|
-
column_datetime : 指定日期格式的列,规则与 column_index 一样
|
|
44
|
-
返回的数据一定是一个 list
|
|
45
|
-
"""
|
|
46
|
-
if file_name.endswith('.xls') or file_name.endswith('.xlsx'):
|
|
47
|
-
return to_list_from_excel(file_name=file_name,
|
|
48
|
-
sheet_index=sheet_index,
|
|
49
|
-
column_index=column_index,
|
|
50
|
-
column_date=column_date,
|
|
51
|
-
column_datetime=column_datetime)
|
|
52
|
-
return to_list_from_txt(file_name=file_name,
|
|
53
|
-
sep=sep,
|
|
54
|
-
sep_line=sep_line,
|
|
55
|
-
sep_line_contain=sep_line_contain,
|
|
56
|
-
sep_line_prefix=sep_line_prefix,
|
|
57
|
-
sep_line_suffix=sep_line_suffix,
|
|
58
|
-
sep_all=sep_all,
|
|
59
|
-
line_ignore_start_with=line_ignore_start_with,
|
|
60
|
-
line_ignore_end_with=line_ignore_end_with,
|
|
61
|
-
line_ignore_empty=line_ignore_empty,
|
|
62
|
-
start_index=start_index,
|
|
63
|
-
start_line=start_line,
|
|
64
|
-
end_index=end_index,
|
|
65
|
-
end_line=end_line,
|
|
66
|
-
count=count)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def to_list_from_excel(file_name: str = 'a.xls',
|
|
70
|
-
sheet_index: int = 1,
|
|
71
|
-
column_index: list | int | str | None = None,
|
|
72
|
-
column_date: list | int | str | None = None,
|
|
73
|
-
column_datetime: list | int | str | None = None) -> list:
|
|
74
|
-
"""
|
|
75
|
-
当读取 excel 之类的文件的时候
|
|
76
|
-
将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
|
|
77
|
-
sheet_index : 从 1 开始编号,
|
|
78
|
-
column_index : 从 1 开始编号, 指定列, 如果是指定值是一个, 这个时候返回的是一个 list, 没有嵌套 list
|
|
79
|
-
如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
|
|
80
|
-
column_date : 指定日期格式的列,规则与 column_index 一样
|
|
81
|
-
column_datetime : 指定日期格式的列,规则与 column_index 一样
|
|
82
|
-
"""
|
|
83
|
-
if file_is_empty(file_name):
|
|
84
|
-
return []
|
|
85
|
-
data_list = list()
|
|
86
|
-
# excel 表格解析成 list 数据
|
|
87
|
-
list_index = []
|
|
88
|
-
for one_index in [column_index, column_date, column_datetime]:
|
|
89
|
-
list_index_one = None
|
|
90
|
-
if one_index is not None:
|
|
91
|
-
list_index_one = []
|
|
92
|
-
if isinstance(one_index, int):
|
|
93
|
-
list_index_one.append(one_index)
|
|
94
|
-
if isinstance(one_index, str):
|
|
95
|
-
i_list = one_index.split(',')
|
|
96
|
-
for i in i_list:
|
|
97
|
-
list_index_one.append(int(i))
|
|
98
|
-
if isinstance(one_index, list):
|
|
99
|
-
for i in one_index:
|
|
100
|
-
list_index_one.append(int(i))
|
|
101
|
-
list_index.append(list_index_one)
|
|
102
|
-
list_all = []
|
|
103
|
-
for one_list in list_index:
|
|
104
|
-
if one_list is not None:
|
|
105
|
-
for o in one_list:
|
|
106
|
-
list_all.append(o)
|
|
107
|
-
if len(list_all) > 0 and list_index[0] is not None:
|
|
108
|
-
list_index[0] = list_all
|
|
109
|
-
# 是否是单 list 类型的数据
|
|
110
|
-
list_only_one = False
|
|
111
|
-
if list_index[0] is not None and len(list_index[0]) == 1:
|
|
112
|
-
list_only_one = True
|
|
113
|
-
# 是 xls 格式
|
|
114
|
-
if file_name.endswith('.xls'):
|
|
115
|
-
book = xlrd.open_workbook(file_name) # 打开一个excel
|
|
116
|
-
sheet = book.sheet_by_index(sheet_index - 1) # 根据顺序获取sheet
|
|
117
|
-
for i in range(sheet.nrows): # 0 1 2 3 4 5
|
|
118
|
-
rows = sheet.row_values(i)
|
|
119
|
-
row_data = []
|
|
120
|
-
for j in range(len(rows)):
|
|
121
|
-
cell_data = str(rows[j]).strip()
|
|
122
|
-
is_date = False
|
|
123
|
-
is_datetime = False
|
|
124
|
-
# 日期格式的列
|
|
125
|
-
if list_index[1] is not None and j + 1 in list_index[1]:
|
|
126
|
-
cell_data = to_date(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
|
|
127
|
-
is_date = True
|
|
128
|
-
row_data.append(cell_data)
|
|
129
|
-
if list_only_one:
|
|
130
|
-
row_data = cell_data
|
|
131
|
-
# 日期时间格式的列
|
|
132
|
-
if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
|
|
133
|
-
cell_data = to_datetime(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
|
|
134
|
-
is_datetime = True
|
|
135
|
-
row_data.append(cell_data)
|
|
136
|
-
if list_only_one:
|
|
137
|
-
row_data = cell_data
|
|
138
|
-
# 指定需要的列
|
|
139
|
-
if not is_date and not is_datetime:
|
|
140
|
-
if list_index[0] is None:
|
|
141
|
-
row_data.append(cell_data)
|
|
142
|
-
else:
|
|
143
|
-
# 指定需要的列
|
|
144
|
-
if j + 1 in list_index[0]:
|
|
145
|
-
row_data.append(cell_data)
|
|
146
|
-
if list_only_one:
|
|
147
|
-
row_data = cell_data
|
|
148
|
-
data_list.append(row_data)
|
|
149
|
-
# 是 xlsx 格式
|
|
150
|
-
if file_name.endswith('.xlsx'):
|
|
151
|
-
wb = openpyxl.load_workbook(filename=file_name, read_only=True)
|
|
152
|
-
ws = wb[wb.sheetnames[sheet_index - 1]]
|
|
153
|
-
for rows in ws.rows:
|
|
154
|
-
row_data = []
|
|
155
|
-
for j in range(len(rows)):
|
|
156
|
-
cell_data = str(rows[j].value).strip()
|
|
157
|
-
is_date = False
|
|
158
|
-
is_datetime = False
|
|
159
|
-
# 日期格式的列
|
|
160
|
-
if list_index[1] is not None and j + 1 in list_index[1]:
|
|
161
|
-
cell_data = to_date(cell_data)
|
|
162
|
-
is_date = True
|
|
163
|
-
row_data.append(cell_data)
|
|
164
|
-
if list_only_one:
|
|
165
|
-
row_data = cell_data
|
|
166
|
-
# 日期时间格式的列
|
|
167
|
-
if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
|
|
168
|
-
cell_data = to_datetime(cell_data)
|
|
169
|
-
is_datetime = True
|
|
170
|
-
row_data.append(cell_data)
|
|
171
|
-
if list_only_one:
|
|
172
|
-
row_data = cell_data
|
|
173
|
-
# 指定需要的列
|
|
174
|
-
if not is_date and not is_datetime:
|
|
175
|
-
if list_index[0] is None:
|
|
176
|
-
row_data.append(cell_data)
|
|
177
|
-
else:
|
|
178
|
-
# 指定需要的列
|
|
179
|
-
if j + 1 in list_index[0]:
|
|
180
|
-
row_data.append(cell_data)
|
|
181
|
-
if list_only_one:
|
|
182
|
-
row_data = cell_data
|
|
183
|
-
data_list.append(row_data)
|
|
184
|
-
return data_list
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
def to_list_from_txt_with_blank_line(file_name: str = 'a.txt') -> list:
|
|
188
|
-
"""
|
|
189
|
-
将一个文件中以空行作为分隔符,
|
|
190
|
-
组成一个 list(list) 数据
|
|
191
|
-
多行空行,自动合并到一行空行
|
|
192
|
-
"""
|
|
193
|
-
return to_list_from_txt(file_name, sep_line='')
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
def to_list_json_from_txt(file_name: str = 'a.txt',
|
|
197
|
-
start_index: int = None,
|
|
198
|
-
start_line: str = None,
|
|
199
|
-
start_line_exclude: str | list[str] | set[str] = None,
|
|
200
|
-
end_index: int = None,
|
|
201
|
-
end_line: str = None,
|
|
202
|
-
end_line_exclude: str | list[str] | set[str] = None,
|
|
203
|
-
count: int = None) -> list:
|
|
204
|
-
"""
|
|
205
|
-
将一个文件中的数据按照行来区分,
|
|
206
|
-
会自动过滤掉空格行,
|
|
207
|
-
组成一个 list[json] 数据
|
|
208
|
-
可以将以下文本转 list[json]
|
|
209
|
-
{"accessKey":"1","signature":"4","timestamp":"1747639787"}
|
|
210
|
-
{"accessKey":"2","signature":"5","timestamp":"1747639787"}
|
|
211
|
-
{"accessKey":"3","signature":"6","timestamp":"1747639787"}
|
|
212
|
-
"""
|
|
213
|
-
return to_list_from_txt(file_name,
|
|
214
|
-
start_index=start_index,
|
|
215
|
-
start_line=start_line,
|
|
216
|
-
start_line_exclude=start_line_exclude,
|
|
217
|
-
end_index=end_index,
|
|
218
|
-
end_line=end_line,
|
|
219
|
-
end_line_exclude=end_line_exclude,
|
|
220
|
-
count=count,
|
|
221
|
-
line_json=True)
|
|
222
|
-
|
|
223
|
-
|
|
224
6
|
def to_list_from_txt(file_name: str = 'a.txt',
|
|
225
7
|
sep: str = None,
|
|
226
8
|
sep_line: str = None,
|
|
@@ -464,6 +246,224 @@ def to_list_from_txt(file_name: str = 'a.txt',
|
|
|
464
246
|
return data_list
|
|
465
247
|
|
|
466
248
|
|
|
249
|
+
def to_list_from_excel(file_name: str = 'a.xls',
|
|
250
|
+
sheet_index: int = 1,
|
|
251
|
+
column_index: list | int | str | None = None,
|
|
252
|
+
column_date: list | int | str | None = None,
|
|
253
|
+
column_datetime: list | int | str | None = None) -> list:
|
|
254
|
+
"""
|
|
255
|
+
当读取 excel 之类的文件的时候
|
|
256
|
+
将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
|
|
257
|
+
sheet_index : 从 1 开始编号,
|
|
258
|
+
column_index : 从 1 开始编号, 指定列, 如果是指定值是一个, 这个时候返回的是一个 list, 没有嵌套 list
|
|
259
|
+
如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
|
|
260
|
+
column_date : 指定日期格式的列,规则与 column_index 一样
|
|
261
|
+
column_datetime : 指定日期格式的列,规则与 column_index 一样
|
|
262
|
+
"""
|
|
263
|
+
if file_is_empty(file_name):
|
|
264
|
+
return []
|
|
265
|
+
data_list = list()
|
|
266
|
+
# excel 表格解析成 list 数据
|
|
267
|
+
list_index = []
|
|
268
|
+
for one_index in [column_index, column_date, column_datetime]:
|
|
269
|
+
list_index_one = None
|
|
270
|
+
if one_index is not None:
|
|
271
|
+
list_index_one = []
|
|
272
|
+
if isinstance(one_index, int):
|
|
273
|
+
list_index_one.append(one_index)
|
|
274
|
+
if isinstance(one_index, str):
|
|
275
|
+
i_list = one_index.split(',')
|
|
276
|
+
for i in i_list:
|
|
277
|
+
list_index_one.append(int(i))
|
|
278
|
+
if isinstance(one_index, list):
|
|
279
|
+
for i in one_index:
|
|
280
|
+
list_index_one.append(int(i))
|
|
281
|
+
list_index.append(list_index_one)
|
|
282
|
+
list_all = []
|
|
283
|
+
for one_list in list_index:
|
|
284
|
+
if one_list is not None:
|
|
285
|
+
for o in one_list:
|
|
286
|
+
list_all.append(o)
|
|
287
|
+
if len(list_all) > 0 and list_index[0] is not None:
|
|
288
|
+
list_index[0] = list_all
|
|
289
|
+
# 是否是单 list 类型的数据
|
|
290
|
+
list_only_one = False
|
|
291
|
+
if list_index[0] is not None and len(list_index[0]) == 1:
|
|
292
|
+
list_only_one = True
|
|
293
|
+
# 是 xls 格式
|
|
294
|
+
if file_name.endswith('.xls'):
|
|
295
|
+
book = xlrd.open_workbook(file_name) # 打开一个excel
|
|
296
|
+
sheet = book.sheet_by_index(sheet_index - 1) # 根据顺序获取sheet
|
|
297
|
+
for i in range(sheet.nrows): # 0 1 2 3 4 5
|
|
298
|
+
rows = sheet.row_values(i)
|
|
299
|
+
row_data = []
|
|
300
|
+
for j in range(len(rows)):
|
|
301
|
+
cell_data = str(rows[j]).strip()
|
|
302
|
+
is_date = False
|
|
303
|
+
is_datetime = False
|
|
304
|
+
# 日期格式的列
|
|
305
|
+
if list_index[1] is not None and j + 1 in list_index[1]:
|
|
306
|
+
cell_data = to_date(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
|
|
307
|
+
is_date = True
|
|
308
|
+
row_data.append(cell_data)
|
|
309
|
+
if list_only_one:
|
|
310
|
+
row_data = cell_data
|
|
311
|
+
# 日期时间格式的列
|
|
312
|
+
if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
|
|
313
|
+
cell_data = to_datetime(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
|
|
314
|
+
is_datetime = True
|
|
315
|
+
row_data.append(cell_data)
|
|
316
|
+
if list_only_one:
|
|
317
|
+
row_data = cell_data
|
|
318
|
+
# 指定需要的列
|
|
319
|
+
if not is_date and not is_datetime:
|
|
320
|
+
if list_index[0] is None:
|
|
321
|
+
row_data.append(cell_data)
|
|
322
|
+
else:
|
|
323
|
+
# 指定需要的列
|
|
324
|
+
if j + 1 in list_index[0]:
|
|
325
|
+
row_data.append(cell_data)
|
|
326
|
+
if list_only_one:
|
|
327
|
+
row_data = cell_data
|
|
328
|
+
data_list.append(row_data)
|
|
329
|
+
# 是 xlsx 格式
|
|
330
|
+
if file_name.endswith('.xlsx'):
|
|
331
|
+
wb = openpyxl.load_workbook(filename=file_name, read_only=True)
|
|
332
|
+
ws = wb[wb.sheetnames[sheet_index - 1]]
|
|
333
|
+
for rows in ws.rows:
|
|
334
|
+
row_data = []
|
|
335
|
+
for j in range(len(rows)):
|
|
336
|
+
cell_data = str(rows[j].value).strip()
|
|
337
|
+
is_date = False
|
|
338
|
+
is_datetime = False
|
|
339
|
+
# 日期格式的列
|
|
340
|
+
if list_index[1] is not None and j + 1 in list_index[1]:
|
|
341
|
+
cell_data = to_date(cell_data)
|
|
342
|
+
is_date = True
|
|
343
|
+
row_data.append(cell_data)
|
|
344
|
+
if list_only_one:
|
|
345
|
+
row_data = cell_data
|
|
346
|
+
# 日期时间格式的列
|
|
347
|
+
if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
|
|
348
|
+
cell_data = to_datetime(cell_data)
|
|
349
|
+
is_datetime = True
|
|
350
|
+
row_data.append(cell_data)
|
|
351
|
+
if list_only_one:
|
|
352
|
+
row_data = cell_data
|
|
353
|
+
# 指定需要的列
|
|
354
|
+
if not is_date and not is_datetime:
|
|
355
|
+
if list_index[0] is None:
|
|
356
|
+
row_data.append(cell_data)
|
|
357
|
+
else:
|
|
358
|
+
# 指定需要的列
|
|
359
|
+
if j + 1 in list_index[0]:
|
|
360
|
+
row_data.append(cell_data)
|
|
361
|
+
if list_only_one:
|
|
362
|
+
row_data = cell_data
|
|
363
|
+
data_list.append(row_data)
|
|
364
|
+
return data_list
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def to_list(file_name: str = 'a.txt',
|
|
368
|
+
sep: str = None,
|
|
369
|
+
sep_line: str = None,
|
|
370
|
+
sep_line_contain: str = None,
|
|
371
|
+
sep_line_prefix: str = None,
|
|
372
|
+
sep_line_suffix: str = None,
|
|
373
|
+
sep_all: str = None,
|
|
374
|
+
line_ignore_start_with: list[str] | set[str] | str = None,
|
|
375
|
+
line_ignore_end_with: list[str] | set[str] | str | None = None,
|
|
376
|
+
line_ignore_empty: bool | None = None,
|
|
377
|
+
start_index: int = None,
|
|
378
|
+
start_line: str = None,
|
|
379
|
+
end_index: int = None,
|
|
380
|
+
end_line: str = None,
|
|
381
|
+
count: int = None,
|
|
382
|
+
sheet_index: int = 1,
|
|
383
|
+
column_index: list[str] | set[str] | str | None = None,
|
|
384
|
+
column_date: list[str] | set[str] | str | None = None,
|
|
385
|
+
column_datetime: list[str] | set[str] | str | None = None) -> list:
|
|
386
|
+
"""
|
|
387
|
+
当读取 txt 之类的文件的时候
|
|
388
|
+
将 txt 文件读取到 list 中, 每一行自动过滤掉行前行后的特殊字符
|
|
389
|
+
sep : 是否对每一行进行分割,如果存在这个字段,就分割
|
|
390
|
+
sep_all : 将文件转化成一个字符串,然后对这个字符串,再次总体分割
|
|
391
|
+
start_index : 从这个地方开始读取,从1开始标号 , 包含这一行
|
|
392
|
+
start_line : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 包含这一行
|
|
393
|
+
end_index : 读取到这个地方结束,从1开始标号 , 不包含这一行
|
|
394
|
+
end_line : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行
|
|
395
|
+
count : 读取指定的行数
|
|
396
|
+
##############################################
|
|
397
|
+
当读取 excel 之类的文件的时候
|
|
398
|
+
将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
|
|
399
|
+
sheet : 从 1 开始编号,
|
|
400
|
+
column_index : 从 1 开始编号, 指定列
|
|
401
|
+
column_index : 如果是指定值, 这个时候返回的是一个 list, 没有嵌套 list
|
|
402
|
+
column_index : 如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
|
|
403
|
+
column_date : 指定日期格式的列,规则与 column_index 一样
|
|
404
|
+
column_datetime : 指定日期格式的列,规则与 column_index 一样
|
|
405
|
+
返回的数据一定是一个 list
|
|
406
|
+
"""
|
|
407
|
+
if file_name.endswith('.xls') or file_name.endswith('.xlsx'):
|
|
408
|
+
return to_list_from_excel(file_name=file_name,
|
|
409
|
+
sheet_index=sheet_index,
|
|
410
|
+
column_index=column_index,
|
|
411
|
+
column_date=column_date,
|
|
412
|
+
column_datetime=column_datetime)
|
|
413
|
+
return to_list_from_txt(file_name=file_name,
|
|
414
|
+
sep=sep,
|
|
415
|
+
sep_line=sep_line,
|
|
416
|
+
sep_line_contain=sep_line_contain,
|
|
417
|
+
sep_line_prefix=sep_line_prefix,
|
|
418
|
+
sep_line_suffix=sep_line_suffix,
|
|
419
|
+
sep_all=sep_all,
|
|
420
|
+
line_ignore_start_with=line_ignore_start_with,
|
|
421
|
+
line_ignore_end_with=line_ignore_end_with,
|
|
422
|
+
line_ignore_empty=line_ignore_empty,
|
|
423
|
+
start_index=start_index,
|
|
424
|
+
start_line=start_line,
|
|
425
|
+
end_index=end_index,
|
|
426
|
+
end_line=end_line,
|
|
427
|
+
count=count)
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def to_list_from_txt_with_blank_line(file_name: str = 'a.txt') -> list:
|
|
431
|
+
"""
|
|
432
|
+
将一个文件中以空行作为分隔符,
|
|
433
|
+
组成一个 list(list) 数据
|
|
434
|
+
多行空行,自动合并到一行空行
|
|
435
|
+
"""
|
|
436
|
+
return to_list_from_txt(file_name, sep_line='')
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def to_list_json_from_txt(file_name: str = 'a.txt',
|
|
440
|
+
start_index: int = None,
|
|
441
|
+
start_line: str = None,
|
|
442
|
+
start_line_exclude: str | list[str] | set[str] = None,
|
|
443
|
+
end_index: int = None,
|
|
444
|
+
end_line: str = None,
|
|
445
|
+
end_line_exclude: str | list[str] | set[str] = None,
|
|
446
|
+
count: int = None) -> list:
|
|
447
|
+
"""
|
|
448
|
+
将一个文件中的数据按照行来区分,
|
|
449
|
+
会自动过滤掉空格行,
|
|
450
|
+
组成一个 list[json] 数据
|
|
451
|
+
可以将以下文本转 list[json]
|
|
452
|
+
{"accessKey":"1","signature":"4","timestamp":"1747639787"}
|
|
453
|
+
{"accessKey":"2","signature":"5","timestamp":"1747639787"}
|
|
454
|
+
{"accessKey":"3","signature":"6","timestamp":"1747639787"}
|
|
455
|
+
"""
|
|
456
|
+
return to_list_from_txt(file_name,
|
|
457
|
+
start_index=start_index,
|
|
458
|
+
start_line=start_line,
|
|
459
|
+
start_line_exclude=start_line_exclude,
|
|
460
|
+
end_index=end_index,
|
|
461
|
+
end_line=end_line,
|
|
462
|
+
end_line_exclude=end_line_exclude,
|
|
463
|
+
count=count,
|
|
464
|
+
line_json=True)
|
|
465
|
+
|
|
466
|
+
|
|
467
467
|
# 读取文件中的数据,返回一个 str
|
|
468
468
|
def to_str_from_file(file_name: str = 'a.txt',
|
|
469
469
|
str_join: str = ' ',
|
|
@@ -492,13 +492,16 @@ def to_json_from_file(file_name: str = 'a.txt',
|
|
|
492
492
|
start_line: str = None,
|
|
493
493
|
end_index: int = None,
|
|
494
494
|
end_line: str = None,
|
|
495
|
+
line_ignore_start_with=None,
|
|
495
496
|
count: int = None) -> dict[str, Any]:
|
|
497
|
+
if line_ignore_start_with is None:
|
|
498
|
+
line_ignore_start_with = ['//', '/*', '#']
|
|
496
499
|
return to_data_from_file(file_name=file_name,
|
|
497
500
|
start_index=start_index,
|
|
498
501
|
start_line=start_line,
|
|
499
502
|
end_index=end_index,
|
|
500
503
|
end_line=end_line,
|
|
501
|
-
line_ignore_start_with=
|
|
504
|
+
line_ignore_start_with=line_ignore_start_with,
|
|
502
505
|
count=count,
|
|
503
506
|
r_json=True)
|
|
504
507
|
|
|
@@ -4,10 +4,10 @@ upplib/chart_html.py,sha256=39CTD2LbN7LNN3wtb3n-ojN2r_X7RJMWappPgiLSJG4,23144
|
|
|
4
4
|
upplib/clean_up_msg.py,sha256=yR4D2fIXOT8z9VpClfhBjVnjhexObgNZDhvPu5v_5Y4,19237
|
|
5
5
|
upplib/common_package.py,sha256=2-u66k4WbZnX6-PUaYy8wsblnz41nvtX4Hwo3J5hjO0,832
|
|
6
6
|
upplib/config_data.py,sha256=KuRh0chqILL-2jXOUngVqjFMzkNZ0QFo_EVpBtptMuc,2757
|
|
7
|
-
upplib/datetime_function.py,sha256=
|
|
7
|
+
upplib/datetime_function.py,sha256=98ro3cV0fvHVoUIHRSv7bvVecBXSrnkqEiP1WhJCwTU,9481
|
|
8
8
|
upplib/db.py,sha256=-3ZALQSzP92-zIhye6ckrXIctbHsVmwoLiCVOlTyDyo,7058
|
|
9
9
|
upplib/file_function.py,sha256=OtVGnmUPd7UO6OJgZjlLgzGMXlftGt94o-_C6-aG2k8,10407
|
|
10
|
-
upplib/file_to_text.py,sha256=
|
|
10
|
+
upplib/file_to_text.py,sha256=Qw24mOgwEsOP83P6YVdU1vTvhSH7Xf0HFR5EEi4jGpE,26944
|
|
11
11
|
upplib/format_data.py,sha256=Qxq-OZ8v6HBRJ-tUcNFem1Imbauk3Y5qJTrAGT0CC4I,10153
|
|
12
12
|
upplib/http_util.py,sha256=8Xya5qpeCKGaglxKnirOlOnbnBU3K52FGV5Y9c8HH9k,16119
|
|
13
13
|
upplib/index.py,sha256=hS_luh6lrMvf_RWvZSJk23eFKLHz5BO52fCcwt6QrmA,13833
|
|
@@ -19,8 +19,8 @@ upplib/query_log.py,sha256=BAUAVr3lA_GbDzHNjI5beJPs0ngNmy2gCi5Q6UMdr1s,9238
|
|
|
19
19
|
upplib/redis_tool.py,sha256=I1kOqBwfQWVIOAY-hQaaOn1Zrx8BNlK83u-pk4uHPCA,707
|
|
20
20
|
upplib/text_to_file.py,sha256=a9Fqj1voOZWn2n74IIBjM6CDS62cNyGfB9BRX_m7imo,14918
|
|
21
21
|
upplib/util.py,sha256=K9OAjsbR2R3k0WWPXWWCphxYc25wbPHrW75pweGUSW4,4671
|
|
22
|
-
upplib-3.4.
|
|
23
|
-
upplib-3.4.
|
|
24
|
-
upplib-3.4.
|
|
25
|
-
upplib-3.4.
|
|
26
|
-
upplib-3.4.
|
|
22
|
+
upplib-3.4.3.dist-info/licenses/LICENSE,sha256=WI5JtXXhjcqnIcPllDA1ZtuxNnZ515xjElcILo7z28o,1073
|
|
23
|
+
upplib-3.4.3.dist-info/METADATA,sha256=7_Q0nh3FZZOXTQJVcEUJ4mJEvgPFdXEfyZTGoNWVYvw,940
|
|
24
|
+
upplib-3.4.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
25
|
+
upplib-3.4.3.dist-info/top_level.txt,sha256=VwdHDDPP79e1LqtRu5_w30hHB4gT0zlj1weuQYOqFoA,7
|
|
26
|
+
upplib-3.4.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|