upplib 3.1.7__py3-none-any.whl → 3.3.1__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/file_text.py ADDED
@@ -0,0 +1,977 @@
1
+ from upplib import *
2
+ from upplib.common_package import *
3
+ from upplib.file import get_file
4
+
5
+
6
+ def get_latest_file(file_path: str = None,
7
+ path_prefix: str = None,
8
+ prefix: str = None,
9
+ path_contain: str = None,
10
+ contain: str = None,
11
+ path_suffix: str = None,
12
+ suffix: str = None,
13
+ full_path: bool = None) -> None | tuple[str, str] | str:
14
+ """
15
+ 按照文件名字排序,获得最新的一个文件
16
+ full_path: 是否返回完整的路径
17
+ """
18
+ html_list = get_file(file_path=file_path,
19
+ path_prefix=path_prefix,
20
+ prefix=prefix,
21
+ path_contain=path_contain,
22
+ contain=contain,
23
+ sort_asc=False,
24
+ path_suffix=path_suffix,
25
+ suffix=suffix)
26
+ r1 = html_list[-1] if len(html_list) > 0 else None
27
+ if r1 is None:
28
+ return None
29
+ r1_short = r1
30
+ file_sep = '\\' if is_win() else '/'
31
+ if file_sep in r1:
32
+ r1_short = r1.split(file_sep)[-1]
33
+ if full_path is None:
34
+ return r1_short, r1
35
+ return r1 if full_path else r1_short
36
+
37
+
38
+ def to_print(*args,
39
+ time_prefix: bool = False,
40
+ line_with_space_count: int = None,
41
+ interval: int = None) -> str:
42
+ """
43
+ 记录日志, 如果是对象会转化为 json
44
+ 数据直接 print, 不记录到文件
45
+ 例如: aaa
46
+ interval: 间隔一段时间,打印一下, 单位: 秒,不要频繁打印
47
+ time_prefix : 是否在前面加时间, 默认 False
48
+ """
49
+ d = ' '.join(map(lambda x: json.dumps(x) if is_json_serializable(x) else str(x), args))
50
+ d = d.strip()
51
+ lo = datetime.today().strftime('%Y-%m-%d %H:%M:%S') + ' ' + d if time_prefix is True else d
52
+ if lo is None or str(lo) == '':
53
+ lo = to_datetime(r_str=True)
54
+ prefix_space = ' ' * (line_with_space_count or 0)
55
+ if interval is None or get_timestamp() - get_thread_local_index_data().get('to_print_time', 0) >= interval:
56
+ get_thread_local_index_data()['to_print_time'] = get_timestamp()
57
+ s = ''
58
+ if interval is not None:
59
+ s = str(to_datetime()) + ' '
60
+ print(prefix_space + s + str(lo))
61
+ return prefix_space + lo
62
+
63
+
64
+ def to_log(*args,
65
+ time_prefix: bool = False,
66
+ line_with_space_count: int = None,
67
+ interval: int = None) -> str:
68
+ """
69
+ 记录日志, 如果是对象会转化为 json
70
+ 前面加了时间
71
+ 例如: 2024-11-07 10:23:47 aaa
72
+ """
73
+ return to_print(*args,
74
+ time_prefix=time_prefix if time_prefix is not None else True,
75
+ line_with_space_count=line_with_space_count,
76
+ interval=interval)
77
+
78
+
79
+ def to_print_file(*args,
80
+ file_path: str = None,
81
+ file_name: str = None,
82
+ file_name_with_date: bool = False,
83
+ file_name_prefix: str = None,
84
+ file_name_suffix: str = None,
85
+ line_with_space_count: int = None,
86
+ mode: str = 'a',
87
+ interval: int = None) -> str:
88
+ """
89
+ 将 print 数据, 写入到 print_file 文件
90
+ 文件按照 日期自动创建
91
+ 例如: print_file/2020-01-01.txt
92
+ to_print_file(query_string, mode='w', file_path=file_path, file_name=get_file_name(file_name=file_path, is_date=True))
93
+ """
94
+ [file_path, file_name, file_name_prefix, file_name_suffix, interval,
95
+ line_with_space_count] = get_and_save_data_to_thread(
96
+ file_path=file_path,
97
+ file_name=file_name,
98
+ file_name_prefix=file_name_prefix,
99
+ file_name_suffix=file_name_suffix,
100
+ interval=interval,
101
+ mode=mode,
102
+ file_name_with_date=file_name_with_date,
103
+ line_with_space_count=line_with_space_count,
104
+ fun_name=to_print_file
105
+ )
106
+ return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
107
+ file_name=('' if file_name_prefix is None else file_name_prefix)
108
+ + (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
109
+ + ('' if file_name_suffix is None else file_name_suffix),
110
+ file_path=str(file_path if file_path is not None else 'to_print_file'),
111
+ mode=mode,
112
+ fixed_name=True,
113
+ suffix='.txt')
114
+
115
+
116
+ def to_print_txt(*args,
117
+ file_path: str = None,
118
+ file_name_with_date: bool = False,
119
+ file_name: str = None,
120
+ file_name_prefix: str = None,
121
+ file_name_suffix: str = None,
122
+ line_with_space_count: int = None,
123
+ mode: str = 'a',
124
+ interval: int = None) -> str:
125
+ """
126
+ 将 print 数据, 写入到 print_txt 文件
127
+ 文件按照 日期自动创建
128
+ 例如: print_txt/2020-01-01.txt
129
+ """
130
+ [file_path, file_name, file_name_prefix, file_name_suffix, interval,
131
+ line_with_space_count] = get_and_save_data_to_thread(
132
+ file_path=file_path,
133
+ file_name=file_name,
134
+ file_name_prefix=file_name_prefix,
135
+ file_name_suffix=file_name_suffix,
136
+ interval=interval,
137
+ mode=mode,
138
+ file_name_with_date=file_name_with_date,
139
+ line_with_space_count=line_with_space_count,
140
+ fun_name=to_print_txt
141
+ )
142
+ return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
143
+ file_name=('' if file_name_prefix is None else file_name_prefix)
144
+ + (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
145
+ + ('' if file_name_suffix is None else file_name_suffix),
146
+ file_path=str(file_path if file_path is not None else 'to_print_txt'),
147
+ mode=mode,
148
+ fixed_name=True,
149
+ suffix='.txt')
150
+
151
+
152
+ def to_log_file(*args,
153
+ file_path: str = None,
154
+ file_name_with_date: bool = False,
155
+ file_name: str = None,
156
+ file_name_prefix: str = None,
157
+ file_name_suffix: str = None,
158
+ line_with_space_count: int = None,
159
+ time_prefix: bool = True,
160
+ mode: str = 'a',
161
+ interval: int = None) -> None:
162
+ """
163
+ 将 log 数据, 写入到 log_file 文件
164
+ 文件按照 日期自动创建
165
+ 例如: log_file/2020-01-01.log
166
+ """
167
+ [file_path, file_name, file_name_prefix, file_name_suffix, interval,
168
+ line_with_space_count] = get_and_save_data_to_thread(
169
+ file_path=file_path,
170
+ file_name=file_name,
171
+ file_name_prefix=file_name_prefix,
172
+ file_name_suffix=file_name_suffix,
173
+ interval=interval,
174
+ mode=mode,
175
+ file_name_with_date=file_name_with_date,
176
+ line_with_space_count=line_with_space_count,
177
+ fun_name=to_log_file
178
+ )
179
+ to_txt(data_param=[
180
+ to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
181
+ file_name=('' if file_name_prefix is None else file_name_prefix)
182
+ + (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
183
+ + ('' if file_name_suffix is None else file_name_suffix),
184
+ file_path=str(file_path if file_path is not None else 'to_log_file'),
185
+ fixed_name=True,
186
+ mode=mode,
187
+ suffix='.log')
188
+
189
+
190
+ def to_log_txt(*args,
191
+ file_path: str = None,
192
+ file_name_with_date: bool = False,
193
+ file_name: str = None,
194
+ file_name_prefix: str = None,
195
+ file_name_suffix: str = None,
196
+ line_with_space_count: int = None,
197
+ time_prefix: bool = True,
198
+ mode: str = 'a',
199
+ interval: int = None) -> None:
200
+ """
201
+ 将 log 数据, 写入到 log_txt 文件夹中
202
+ 文件按照 日期自动创建
203
+ 例如: log_txt/2020-01-01.txt
204
+ """
205
+ [file_path, file_name, file_name_prefix, file_name_suffix, interval,
206
+ line_with_space_count] = get_and_save_data_to_thread(
207
+ file_path=file_path,
208
+ file_name=file_name,
209
+ file_name_prefix=file_name_prefix,
210
+ file_name_suffix=file_name_suffix,
211
+ interval=interval,
212
+ mode=mode,
213
+ file_name_with_date=file_name_with_date,
214
+ line_with_space_count=line_with_space_count,
215
+ fun_name=to_log_txt
216
+ )
217
+ to_txt(data_param=[
218
+ to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
219
+ file_name=('' if file_name_prefix is None else file_name_prefix)
220
+ + (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
221
+ + ('' if file_name_suffix is None else file_name_suffix),
222
+ file_path=str(file_path if file_path is not None else 'to_log_txt'),
223
+ mode=mode,
224
+ fixed_name=True,
225
+ suffix='.txt')
226
+
227
+
228
+ def check_file(file_name: str = None) -> None:
229
+ r"""
230
+ 检查文件夹是否存在,不存在,就创建新的
231
+ 支持多级目录 , 例如: C:\Users\yangpu\Desktop\study\a\b\c\d\e\f
232
+ """
233
+ if file_name is None or file_name == '':
234
+ return
235
+ file_sep = '\\' if is_win() else '/'
236
+ f_n = file_name.split(file_sep)
237
+ for i in range(1, len(f_n) + 1):
238
+ # C:\Users\yangpu\Desktop\study\p.t
239
+ p_n = file_sep.join(f_n[0:i])
240
+ if p_n and not os.path.exists(p_n):
241
+ os.mkdir(p_n)
242
+
243
+
244
+ def to_txt(data_param: Any,
245
+ file_name: str = 'txt',
246
+ file_path: str = 'txt',
247
+ fixed_name: bool = False,
248
+ mode: str = 'a',
249
+ suffix: str = '.txt',
250
+ sep_list: str = '\t',
251
+ file_name_is_date: bool = False) -> str:
252
+ r"""
253
+ 将 list 中的数据以 json 或者基本类型的形式写入到文件中
254
+ data_param : 数组数据, 也可以不是数组
255
+ file_name : 文件名 , 默认 txt
256
+ 当文件名是 C:\Users\yangpu\Desktop\study\abc\d\e\f\a.sql 这种类型的时候, 可以直接创建文件夹,
257
+ 会赋值 file_name=a,
258
+ file_path=C:\Users\yangpu\Desktop\study\abc\d\e\f,
259
+ fixed_name=True,
260
+ suffix=.sql
261
+ 当文件名是 abc 的时候, 按照正常值,计算
262
+ file_path : 文件路径
263
+ fixed_name : 是否固定文件名
264
+ suffix : 文件后缀, 默认 .txt
265
+ sep_list : 当 data_param 是 list(list) 类型的时候 使用 sep_list 作为分割内部的分隔符,
266
+ 默认使用 \t 作为分隔符, 如果为 None , 则按照 json 去处理这个 list
267
+ """
268
+ file_name = str(file_name)
269
+ for sep in ['\\', '/']:
270
+ f_n = file_name.split(sep)
271
+ if len(f_n) > 1:
272
+ file_name = f_n[-1]
273
+ file_path = sep.join(f_n[0:-1])
274
+ if '.' in file_name:
275
+ suffix = '.' + file_name.split('.')[-1]
276
+ file_name = file_name[0:file_name.rfind('.')]
277
+ fixed_name = True
278
+
279
+ # 检查路径 file_path
280
+ while file_path.endswith('/'):
281
+ file_path = file_path[0:-1]
282
+ check_file(file_path)
283
+
284
+ # 在 file_name 中, 检查是否有后缀
285
+ if '.' in file_name:
286
+ suffix = '.' + file_name.split('.')[-1]
287
+ file_name = file_name[0:file_name.rfind('.')]
288
+
289
+ # 生成 file_name
290
+ if fixed_name:
291
+ file_name = file_name + suffix
292
+ else:
293
+ file_name = get_file_name(file_name, suffix, is_date=file_name_is_date)
294
+ # 文件路径
295
+ file_name_path = file_name
296
+ if file_path != '':
297
+ file_name_path = file_path + '/' + file_name
298
+ # 写入文件
299
+ text_file = open(file_name_path, mode, encoding='utf-8')
300
+ if isinstance(data_param, set):
301
+ data_param = list(data_param)
302
+ if not isinstance(data_param, list):
303
+ text_file.write(to_str(data_param) + '\n')
304
+ else:
305
+ for one in data_param:
306
+ if isinstance(one, (list, tuple, set)) and sep_list is not None:
307
+ text_file.write(str(sep_list).join(list(map(lambda x: to_str(x), one))) + '\n')
308
+ else:
309
+ text_file.write(to_str(one) + '\n')
310
+ text_file.close()
311
+ return file_name_path
312
+
313
+
314
+ # 将 list 中的数据写入到固定的文件中,自己设置文件后缀
315
+ def to_txt_file(data_param: Any,
316
+ file_name: str = None,
317
+ mode: str = 'a') -> str:
318
+ file_name = get_file_name('to_txt_file', is_date=True) if file_name is None else file_name
319
+ suffix = '.txt'
320
+ f = file_name
321
+ for sep in ['\\', '/']:
322
+ f_n = file_name.split(sep)
323
+ if len(f_n) > 1:
324
+ f = file_name
325
+ if '.' in f:
326
+ suffix = '.' + f.split('.')[-1]
327
+ file_name = file_name.replace(suffix, '')
328
+ return to_txt(data_param=data_param,
329
+ file_name=file_name,
330
+ file_path='to_txt_file',
331
+ suffix=suffix,
332
+ fixed_name=True,
333
+ mode=mode)
334
+
335
+
336
+ # 将 list 中的数据写入到固定的文件中,自己设置文件后缀
337
+ def to_file(data_param: Any,
338
+ file_name: str = None,
339
+ mode: str = 'a') -> str:
340
+ file_name = get_file_name('to_file', is_date=True) if file_name is None else file_name
341
+ suffix = '.txt'
342
+ f = file_name
343
+ for sep in ['\\', '/']:
344
+ f_n = file_name.split(sep)
345
+ if len(f_n) > 1:
346
+ f = file_name
347
+ if '.' in f:
348
+ suffix = '.' + f.split('.')[-1]
349
+ file_name = file_name.replace(suffix, '')
350
+ return to_txt(data_param=data_param,
351
+ file_name=file_name,
352
+ file_path='file',
353
+ suffix=suffix,
354
+ fixed_name=True,
355
+ mode=mode)
356
+
357
+
358
+ def to_list(file_name: str = 'a.txt',
359
+ sep: str = None,
360
+ sep_line: str = None,
361
+ sep_line_contain: str = None,
362
+ sep_line_prefix: str = None,
363
+ sep_line_suffix: str = None,
364
+ sep_all: str = None,
365
+ line_ignore_start_with: list[str] | set[str] | str = None,
366
+ line_ignore_end_with: list[str] | set[str] | str | None = None,
367
+ start_index: int = None,
368
+ start_line: str = None,
369
+ end_index: int = None,
370
+ end_line: str = None,
371
+ count: int = None,
372
+ sheet_index: int = 1,
373
+ column_index: list[str] | set[str] | str | None = None,
374
+ column_date: list[str] | set[str] | str | None = None,
375
+ column_datetime: list[str] | set[str] | str | None = None) -> list:
376
+ """
377
+ 当读取 txt 之类的文件的时候
378
+ 将 txt 文件读取到 list 中, 每一行自动过滤掉行前行后的特殊字符
379
+ sep : 是否对每一行进行分割,如果存在这个字段,就分割
380
+ sep_all : 将文件转化成一个字符串,然后对这个字符串,再次总体分割
381
+ start_index : 从这个地方开始读取,从1开始标号 , 包含这一行
382
+ start_line : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 包含这一行
383
+ end_index : 读取到这个地方结束,从1开始标号 , 不包含这一行
384
+ end_line : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行
385
+ count : 读取指定的行数
386
+ ##############################################
387
+ 当读取 excel 之类的文件的时候
388
+ 将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
389
+ sheet : 从 1 开始编号,
390
+ column_index : 从 1 开始编号, 指定列
391
+ column_index : 如果是指定值, 这个时候返回的是一个 list, 没有嵌套 list
392
+ column_index : 如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
393
+ column_date : 指定日期格式的列,规则与 column_index 一样
394
+ column_datetime : 指定日期格式的列,规则与 column_index 一样
395
+ 返回的数据一定是一个 list
396
+ """
397
+ if file_name.endswith('.xls') or file_name.endswith('.xlsx'):
398
+ return to_list_from_excel(file_name=file_name,
399
+ sheet_index=sheet_index,
400
+ column_index=column_index,
401
+ column_date=column_date,
402
+ column_datetime=column_datetime)
403
+ return to_list_from_txt(file_name=file_name,
404
+ sep=sep,
405
+ sep_line=sep_line,
406
+ sep_line_contain=sep_line_contain,
407
+ sep_line_prefix=sep_line_prefix,
408
+ sep_line_suffix=sep_line_suffix,
409
+ sep_all=sep_all,
410
+ line_ignore_start_with=line_ignore_start_with,
411
+ line_ignore_end_with=line_ignore_end_with,
412
+ start_index=start_index,
413
+ start_line=start_line,
414
+ end_index=end_index,
415
+ end_line=end_line,
416
+ count=count)
417
+
418
+
419
+ def to_list_from_excel(file_name: str = 'a.xls',
420
+ sheet_index: int = 1,
421
+ column_index: list | int | str | None = None,
422
+ column_date: list | int | str | None = None,
423
+ column_datetime: list | int | str | None = None) -> list:
424
+ """
425
+ 当读取 excel 之类的文件的时候
426
+ 将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
427
+ sheet_index : 从 1 开始编号,
428
+ column_index : 从 1 开始编号, 指定列, 如果是指定值是一个, 这个时候返回的是一个 list, 没有嵌套 list
429
+ 如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
430
+ column_date : 指定日期格式的列,规则与 column_index 一样
431
+ column_datetime : 指定日期格式的列,规则与 column_index 一样
432
+ """
433
+ if file_is_empty(file_name):
434
+ return []
435
+ data_list = list()
436
+ # excel 表格解析成 list 数据
437
+ list_index = []
438
+ for one_index in [column_index, column_date, column_datetime]:
439
+ list_index_one = None
440
+ if one_index is not None:
441
+ list_index_one = []
442
+ if isinstance(one_index, int):
443
+ list_index_one.append(one_index)
444
+ if isinstance(one_index, str):
445
+ i_list = one_index.split(',')
446
+ for i in i_list:
447
+ list_index_one.append(int(i))
448
+ if isinstance(one_index, list):
449
+ for i in one_index:
450
+ list_index_one.append(int(i))
451
+ list_index.append(list_index_one)
452
+ list_all = []
453
+ for one_list in list_index:
454
+ if one_list is not None:
455
+ for o in one_list:
456
+ list_all.append(o)
457
+ if len(list_all) > 0 and list_index[0] is not None:
458
+ list_index[0] = list_all
459
+ # 是否是单 list 类型的数据
460
+ list_only_one = False
461
+ if list_index[0] is not None and len(list_index[0]) == 1:
462
+ list_only_one = True
463
+ # 是 xls 格式
464
+ if file_name.endswith('.xls'):
465
+ book = xlrd.open_workbook(file_name) # 打开一个excel
466
+ sheet = book.sheet_by_index(sheet_index - 1) # 根据顺序获取sheet
467
+ for i in range(sheet.nrows): # 0 1 2 3 4 5
468
+ rows = sheet.row_values(i)
469
+ row_data = []
470
+ for j in range(len(rows)):
471
+ cell_data = str(rows[j]).strip()
472
+ is_date = False
473
+ is_datetime = False
474
+ # 日期格式的列
475
+ if list_index[1] is not None and j + 1 in list_index[1]:
476
+ cell_data = to_date(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
477
+ is_date = True
478
+ row_data.append(cell_data)
479
+ if list_only_one:
480
+ row_data = cell_data
481
+ # 日期时间格式的列
482
+ if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
483
+ cell_data = to_datetime(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
484
+ is_datetime = True
485
+ row_data.append(cell_data)
486
+ if list_only_one:
487
+ row_data = cell_data
488
+ # 指定需要的列
489
+ if not is_date and not is_datetime:
490
+ if list_index[0] is None:
491
+ row_data.append(cell_data)
492
+ else:
493
+ # 指定需要的列
494
+ if j + 1 in list_index[0]:
495
+ row_data.append(cell_data)
496
+ if list_only_one:
497
+ row_data = cell_data
498
+ data_list.append(row_data)
499
+ # 是 xlsx 格式
500
+ if file_name.endswith('.xlsx'):
501
+ wb = openpyxl.load_workbook(filename=file_name, read_only=True)
502
+ ws = wb[wb.sheetnames[sheet_index - 1]]
503
+ for rows in ws.rows:
504
+ row_data = []
505
+ for j in range(len(rows)):
506
+ cell_data = str(rows[j].value).strip()
507
+ is_date = False
508
+ is_datetime = False
509
+ # 日期格式的列
510
+ if list_index[1] is not None and j + 1 in list_index[1]:
511
+ cell_data = to_date(cell_data)
512
+ is_date = True
513
+ row_data.append(cell_data)
514
+ if list_only_one:
515
+ row_data = cell_data
516
+ # 日期时间格式的列
517
+ if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
518
+ cell_data = to_datetime(cell_data)
519
+ is_datetime = True
520
+ row_data.append(cell_data)
521
+ if list_only_one:
522
+ row_data = cell_data
523
+ # 指定需要的列
524
+ if not is_date and not is_datetime:
525
+ if list_index[0] is None:
526
+ row_data.append(cell_data)
527
+ else:
528
+ # 指定需要的列
529
+ if j + 1 in list_index[0]:
530
+ row_data.append(cell_data)
531
+ if list_only_one:
532
+ row_data = cell_data
533
+ data_list.append(row_data)
534
+ return data_list
535
+
536
+
537
+ def to_list_from_txt_with_blank_line(file_name: str = 'a.txt') -> list:
538
+ """
539
+ 将一个文件中以空行作为分隔符,
540
+ 组成一个 list(list) 数据
541
+ 多行空行,自动合并到一行空行
542
+ """
543
+ return to_list_from_txt(file_name, sep_line='')
544
+
545
+
546
+ def to_list_json_from_txt(file_name: str = 'a.txt',
547
+ start_index: int = None,
548
+ start_line: str = None,
549
+ start_line_exclude: str | list[str] | set[str] = None,
550
+ end_index: int = None,
551
+ end_line: str = None,
552
+ end_line_exclude: str | list[str] | set[str] = None,
553
+ count: int = None) -> list:
554
+ """
555
+ 将一个文件中的数据按照行来区分,
556
+ 会自动过滤掉空格行,
557
+ 组成一个 list[json] 数据
558
+ 可以将以下文本转 list[json]
559
+ {"accessKey":"1","signature":"4","timestamp":"1747639787"}
560
+ {"accessKey":"2","signature":"5","timestamp":"1747639787"}
561
+ {"accessKey":"3","signature":"6","timestamp":"1747639787"}
562
+ """
563
+ return to_list_from_txt(file_name,
564
+ start_index=start_index,
565
+ start_line=start_line,
566
+ start_line_exclude=start_line_exclude,
567
+ end_index=end_index,
568
+ end_line=end_line,
569
+ end_line_exclude=end_line_exclude,
570
+ count=count,
571
+ line_json=True)
572
+
573
+
574
+ def to_list_from_txt(file_name: str = 'a.txt',
575
+ sep: str = None,
576
+ sep_line: str = None,
577
+ sep_line_contain: str = None,
578
+ sep_line_prefix: str = None,
579
+ sep_line_suffix: str = None,
580
+ sep_line_with_space_count: int = None,
581
+ sep_is_front: bool = True,
582
+ sep_all: str = None,
583
+ line_ignore_start_with: list | int | str = None,
584
+ line_ignore_end_with: list | int | str = None,
585
+ line_ignore_empty: bool | None = None,
586
+ line_join: str = None,
587
+ line_must_start_with: str = None,
588
+ line_must_contain: str = None,
589
+ line_json: bool = None,
590
+ start_index: int = None,
591
+ start_line: str = None,
592
+ start_line_exclude: str | list[str] | set[str] = None,
593
+ end_index: int = None,
594
+ end_line: str = None,
595
+ end_line_exclude: str | list[str] | set[str] = None,
596
+ count: int = None) -> list:
597
+ """
598
+ 将 txt 文件转化成 list 的方法
599
+ 当读取 txt 之类的文件的时候
600
+ 将 txt 文件读取到 list 中, 每一行自动过滤掉行前行后的特殊字符
601
+ sep : 对每一行进行分割,将 list(str) 转化为 list(list(str)), 或者将 list(list(str)) 转化为 list(list(list(str)))
602
+ sep_line : 这一行是一个分隔符, 分隔符与这行一样, 将 list(str) 转化为 list(list(str))
603
+ sep_line_with_space_count : 分隔符是空格的个数, 将 list(str) 转化为 list(list(str))
604
+ sep_line_contain : 这一行是一个分隔符,包含这个行分隔符的做分割, 将 list(str) 转化为 list(list(str))
605
+ sep_line_prefix : 这一行是一个分隔符,以这个分隔符作为前缀的, 将 list(str) 转化为 list(list(str))
606
+ sep_line_suffix : 这一行是一个分隔符,以这个分隔符作为后缀的, 将 list(str) 转化为 list(list(str))
607
+ sep_is_front : 这一行,分割行,是包含到前面,还是包含到
608
+ sep_all : 将文件转化成一个字符串,然后对这个字符串,再次总体分割 将 list(str) 转化为 str , 然后再次转化成 list(str)
609
+ line_ignore_start_with : 忽略以这个为开头的行
610
+ line_ignore_end_with : 忽略以这个为结尾的行
611
+ line_ignore_empty : 如果这一行为空,就忽略这行
612
+ line_must_start_with : 这一行必须以这个字符串为开始
613
+ line_must_contain : 这一行必须包含这个字符串
614
+ line_join : 将 list(list(str)) 转化成 list(str) 类型的数据
615
+ line_json : 将 list(str) 转化成 list(json) 类型的数据, 会自动过滤掉空格行
616
+ start_index : 从这个地方开始读取,从1开始标号 , 包含这一行
617
+ start_line : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 包含这一行
618
+ start_line_exclude : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 不包含这一行, 返回的是一个 list(' '.join(one_line_list))
619
+ end_index : 读取到这个地方结束,从1开始标号 , 不包含这一行
620
+ end_line : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行
621
+ end_line_exclude : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行, 返回的是一个 list(' '.join(one_line_list))
622
+ count : 读取指定的行数
623
+ """
624
+ if file_is_empty(file_name=file_name):
625
+ return []
626
+ data_list = []
627
+ # 普通文件的解析
628
+ d_list = open(file_name, 'r', encoding='utf-8').readlines()
629
+ # 数量
630
+ c = 0
631
+ start_flag = None
632
+ end_flag = None
633
+ if start_line is not None:
634
+ start_flag = False
635
+ if end_line is not None:
636
+ end_flag = False
637
+ for i in range(len(d_list)):
638
+ line = d_list[i].strip()
639
+ # 判断开始位置
640
+ if start_index is not None and i + 1 < to_int(start_index):
641
+ continue
642
+ # 判断结束位置
643
+ if end_index is not None and i + 1 >= to_int(end_index):
644
+ continue
645
+ # 判断数量
646
+ if count is not None and c >= to_int(count):
647
+ continue
648
+ # 开始标记位
649
+ if start_flag is not None and not start_flag and line.find(start_line) > -1:
650
+ # 如果有标记位置,就是 True
651
+ start_flag = True
652
+ # 开始标记位
653
+ if end_flag is not None and not end_flag and line.find(end_line) > -1:
654
+ # 如果有标记位置,就是 True
655
+ end_flag = True
656
+ if start_flag is not None and not start_flag:
657
+ # 有开始标记位参数,并且,还没有走到开始标记位
658
+ continue
659
+ elif end_flag is not None and end_flag:
660
+ # 有结束标记位参数,并且,已经走到了结束标记位
661
+ continue
662
+ c += 1
663
+ can_add = True
664
+ if line_ignore_start_with is not None:
665
+ if isinstance(line_ignore_start_with, list) or isinstance(line_ignore_start_with, set):
666
+ for ss in line_ignore_start_with:
667
+ if line.startswith(str(ss)):
668
+ can_add = False
669
+ elif isinstance(line_ignore_start_with, str):
670
+ if line.startswith(str(line_ignore_start_with)):
671
+ can_add = False
672
+ if line_ignore_end_with is not None:
673
+ if isinstance(line_ignore_end_with, list) or isinstance(line_ignore_end_with, set):
674
+ for ss in line_ignore_end_with:
675
+ if line.endswith(str(ss)):
676
+ can_add = False
677
+ elif isinstance(line_ignore_end_with, str):
678
+ if line.endswith(str(line_ignore_end_with)):
679
+ can_add = False
680
+ if line_ignore_empty is not None and line_ignore_empty:
681
+ # 忽略空行
682
+ if len(line) == 0:
683
+ can_add = False
684
+ if line_must_start_with is not None:
685
+ # 必须以这个字符串为开始
686
+ if not line.startswith(str(line_must_start_with)):
687
+ can_add = False
688
+ if line_must_contain is not None:
689
+ # 必须包含这个字符串
690
+ if line.count(str(line_must_contain)) == 0:
691
+ can_add = False
692
+ if can_add:
693
+ data_list.append(line)
694
+
695
+ # 更复杂的切分, 中间的部分 会转成 ' '.join(one_line_list)
696
+ if start_line_exclude is not None and end_line_exclude is not None:
697
+ data_list1 = data_list
698
+ start_flag = False
699
+ start_flag_once = False
700
+ end_flag = False
701
+ one_data_list = []
702
+ data_list = []
703
+ for i in range(len(data_list1)):
704
+ line = data_list1[i].strip()
705
+ # 开始标记位
706
+ if not start_flag:
707
+ if isinstance(start_line_exclude, list) or isinstance(start_line_exclude, set):
708
+ for ss in start_line_exclude:
709
+ if line.find(str(ss)) > -1:
710
+ # 如果有标记位置,就是 True
711
+ start_flag = True
712
+ start_flag_once = True
713
+ else:
714
+ if line.find(str(start_line_exclude)) > -1:
715
+ # 如果有标记位置,就是 True
716
+ start_flag = True
717
+ start_flag_once = True
718
+ # 结束标记位
719
+ if not end_flag:
720
+ if isinstance(end_line_exclude, list) or isinstance(end_line_exclude, set):
721
+ for ss in end_line_exclude:
722
+ if line.find(str(ss)) > -1:
723
+ # 如果有标记位置,就是 True
724
+ end_flag = True
725
+ else:
726
+ if line.find(str(end_line_exclude)) > -1:
727
+ # 如果有标记位置,就是 True
728
+ end_flag = True
729
+ if not start_flag:
730
+ # 有开始标记位参数,并且,还没有走到开始标记位
731
+ continue
732
+ elif end_flag:
733
+ # 有结束标记位参数,并且,已经走到了结束标记位
734
+ start_flag = False
735
+ end_flag = False
736
+ start_flag_once = False
737
+ # print(one_data_list)
738
+ data_list.append(' '.join(one_data_list).strip())
739
+ one_data_list = []
740
+ continue
741
+ # 去掉 start_line_exclude 包含行
742
+ if start_flag_once:
743
+ start_flag_once = False
744
+ line = ''
745
+ if start_flag and not end_flag:
746
+ one_data_list.append(line)
747
+ if len(one_data_list):
748
+ data_list.append(' '.join(one_data_list).strip())
749
+
750
+ if sep_all is not None:
751
+ # 全部划分, 重新分割成 list(str)
752
+ data_list = ''.join(data_list).split(str(sep_all))
753
+ # 有行分隔符, 将会把 list(str) 转化成 list(list)
754
+ if len(list(filter(lambda x: x is not None,
755
+ [sep_line, sep_line_prefix, sep_line_contain, sep_line_suffix, sep_line_with_space_count]))):
756
+ # 当是这种情况的时候,返回的数据结果
757
+ r_list = []
758
+ # 数据中的一行 list 数据
759
+ one_list = []
760
+ # 空格数量
761
+ space_count = 0
762
+ for d_o in data_list:
763
+ space_count = space_count + 1 if not d_o.strip() else 0
764
+ # 过滤掉空行,无效行
765
+ if len(d_o.strip()) and sep_is_front:
766
+ one_list.append(d_o)
767
+ # 这一行, 等于 sep_line
768
+ if ((sep_line is not None and d_o == sep_line) or
769
+ # 这一行, 包含 sep_line_contain
770
+ (sep_line_contain is not None and d_o.find(sep_line_contain) != -1) or
771
+ # 这一行, 是否是以 sep_line_prefix 开头
772
+ (sep_line_prefix is not None and d_o.startswith(sep_line_prefix)) or
773
+ # 这一行, 是否是以 sep_line_suffix 结尾
774
+ (sep_line_suffix is not None and d_o.endswith(sep_line_suffix))):
775
+ if len(one_list):
776
+ r_list.append(one_list)
777
+ one_list = []
778
+ if len(d_o.strip()) and not sep_is_front:
779
+ one_list.append(d_o)
780
+ # 按照空格行的数量来进行分割
781
+ if sep_line_with_space_count is not None and space_count == sep_line_with_space_count:
782
+ if len(one_list):
783
+ r_list.append(one_list)
784
+ one_list = []
785
+ space_count = 0
786
+ # 最后的一条数据,兼容一下
787
+ if len(one_list):
788
+ r_list.append(one_list)
789
+ data_list = r_list
790
+ # 对这个 list 进行行内再次分割
791
+ if sep is not None:
792
+ r_list = []
793
+ for line in data_list:
794
+ # list(str) 情况
795
+ if isinstance(line, str):
796
+ r_list.append(line.split(str(sep)))
797
+ # list(list) 情况
798
+ elif isinstance(line, list):
799
+ a_list = []
800
+ for o_line in line:
801
+ a_list.append(o_line.split(str(sep)))
802
+ r_list.append(a_list)
803
+ data_list = r_list
804
+ # data_list 中的每一个元素都转化成 str
805
+ if line_join is not None:
806
+ data_list = list(map(lambda x: str(line_join).join(x), data_list))
807
+ # data_list 中的每一个元素都转化成 先转化成str, 然后再转化成json
808
+ if line_json is not None and line_json:
809
+ data_list = list(map(lambda x:
810
+ json.loads(str('' if line_join is None else line_join).join(x)),
811
+ list(filter(lambda x: x is not None and len(str(x)), data_list))
812
+ )
813
+ )
814
+ return data_list
815
+
816
+
817
+ # 读取文件中的数据,返回一个 str
818
+ def to_str_from_file(file_name: str = 'a.txt',
819
+ str_join: str = ' ',
820
+ line_ignore_start_with: list | int | str | None = None,
821
+ line_ignore_end_with: list | int | str | None = None,
822
+ start_index: int = None,
823
+ start_line: str = None,
824
+ end_index: int = None,
825
+ end_line: str = None,
826
+ count: int = None) -> str:
827
+ return to_data_from_file(file_name=file_name,
828
+ line_ignore_start_with=line_ignore_start_with,
829
+ line_ignore_end_with=line_ignore_end_with,
830
+ str_join=str_join,
831
+ start_index=start_index,
832
+ start_line=start_line,
833
+ end_index=end_index,
834
+ end_line=end_line,
835
+ count=count,
836
+ r_str=True)
837
+
838
+
839
+ # 读取文件中的数据,返回一个 json
840
+ def to_json_from_file(file_name: str = 'a.txt',
841
+ start_index: int = None,
842
+ start_line: str = None,
843
+ end_index: int = None,
844
+ end_line: str = None,
845
+ count: int = None) -> dict[str, Any]:
846
+ return to_data_from_file(file_name=file_name,
847
+ start_index=start_index,
848
+ start_line=start_line,
849
+ end_index=end_index,
850
+ end_line=end_line,
851
+ line_ignore_start_with=['//', '/*', '#'],
852
+ count=count,
853
+ r_json=True)
854
+
855
+
856
+ def to_data_from_file(file_name: str = 'a.txt',
857
+ sep: str = None,
858
+ sep_line: str = None,
859
+ sep_all: str = None,
860
+ line_ignore_start_with: list | int | str | None = None,
861
+ line_ignore_end_with: list | int | str | None = None,
862
+ start_index: int = None,
863
+ start_line: str = None,
864
+ end_index: int = None,
865
+ end_line: str = None,
866
+ count: int = None,
867
+ sheet_index: int = 1,
868
+ column_index: list | int | str | None = None,
869
+ column_date: list | int | str | None = None,
870
+ column_datetime: list | int | str | None = None,
871
+ r_json: bool = False,
872
+ str_join: str = '',
873
+ r_str: bool = False) -> str | dict[str, Any]:
874
+ """
875
+ 在 to_list 方法上再嵌套一层,
876
+ r_str : 返回的数据是否是一个 字符串, ''.join(list)
877
+ str_join : 返回的数据是否是一个 字符串, str_join.join(list), 用 str_join 做连接
878
+ r_json : 返回的数据是否是一个 json 类型的数据
879
+ """
880
+ d = to_list(file_name=file_name,
881
+ sep=sep,
882
+ sep_line=sep_line,
883
+ sep_all=sep_all,
884
+ line_ignore_start_with=line_ignore_start_with,
885
+ line_ignore_end_with=line_ignore_end_with,
886
+ start_index=start_index,
887
+ start_line=start_line,
888
+ end_index=end_index,
889
+ end_line=end_line,
890
+ count=count,
891
+ sheet_index=sheet_index,
892
+ column_index=column_index,
893
+ column_date=column_date,
894
+ column_datetime=column_datetime)
895
+ return str_join.join(d) if r_str else json.loads(str_join.join(d)) if r_json else d
896
+
897
+
898
+ # 将文件导出成excel格式的
899
+ def to_excel(data_list: set | list | tuple | None,
900
+ file_name: str = None,
901
+ file_path: str = 'excel') -> None:
902
+ if file_name is None:
903
+ file_name = 'excel'
904
+ file_name = str(file_name)
905
+ while file_path.endswith('/'):
906
+ file_path = file_path[0:-1]
907
+ check_file(file_path)
908
+ # 实例化对象excel对象
909
+ excel_obj = openpyxl.Workbook()
910
+ # excel 内第一个sheet工作表
911
+ excel_obj_sheet = excel_obj[excel_obj.sheetnames[0]]
912
+ # 给单元格赋值
913
+ for one_data in data_list:
914
+ s_list = []
915
+ if isinstance(one_data, list) or isinstance(one_data, set):
916
+ for one in one_data:
917
+ if isinstance(one, dict) or isinstance(one, list):
918
+ s = json.dumps(one)
919
+ else:
920
+ s = str(one)
921
+ s_list.append(s)
922
+ excel_obj_sheet.append(s_list)
923
+ else:
924
+ if is_json_serializable(one_data):
925
+ s = json.dumps(one_data)
926
+ else:
927
+ s = str(one_data)
928
+ excel_obj_sheet.append([s])
929
+
930
+ # 文件保存
931
+ excel_obj.save(file_path + '/' + get_file_name(file_name, '.xlsx', True))
932
+
933
+
934
+ def to_csv(data_list: set | list | tuple | dict,
935
+ file_name: str = None,
936
+ file_path: str = 'csv') -> None:
937
+ """
938
+ 将文件导出成csv格式的
939
+ data_list 格式
940
+ data_list = [['Name', 'Age', 'Gender'],
941
+ ['Alice', 25, 'Female'],
942
+ ['Bob', 30, 'Male'],
943
+ ['Charlie', 35, 'Male']]
944
+ data_list = [{
945
+ "a": 1,
946
+ "b": 2,
947
+ },{
948
+ "a": 1,
949
+ "b": 2,
950
+ }]
951
+ file_name = 'data'
952
+ """
953
+ if file_name is None:
954
+ file_name = 'csv'
955
+ file_name = get_file_name(file_name, '.csv', True)
956
+ while file_path.endswith('/'):
957
+ file_path = file_path[0:-1]
958
+ check_file(file_path)
959
+ d_list = []
960
+ if isinstance(data_list, tuple):
961
+ d_list = list(data_list)
962
+ else:
963
+ if len(data_list) and (isinstance(data_list[0], dict) or isinstance(data_list[0], tuple)):
964
+ title_list = []
965
+ for key in data_list[0]:
966
+ title_list.append(key)
967
+ d_list.append(title_list)
968
+ for one_data in data_list:
969
+ one_list = []
970
+ for k in title_list:
971
+ one_list.append(one_data[k])
972
+ d_list.append(one_list)
973
+ else:
974
+ d_list = data_list
975
+ with open(file_path + '/' + file_name, 'w', newline='') as f:
976
+ writer = csv.writer(f)
977
+ writer.writerows(d_list)