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/__init__.py +3 -0
- upplib/config_data.py +71 -0
- upplib/file.py +70 -49
- upplib/file_text.py +977 -0
- upplib/index.py +0 -1031
- upplib/util.py +2 -3
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/METADATA +1 -1
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/RECORD +11 -9
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/WHEEL +0 -0
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/licenses/LICENSE +0 -0
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/top_level.txt +0 -0
upplib/index.py
CHANGED
|
@@ -2,12 +2,8 @@ from upplib import *
|
|
|
2
2
|
from datetime import datetime, timezone, timedelta
|
|
3
3
|
from typing import Any, Optional, Union
|
|
4
4
|
from upplib.common_package import *
|
|
5
|
-
from upplib.file import get_file
|
|
6
5
|
from collections import defaultdict
|
|
7
6
|
|
|
8
|
-
__CONFIG_PATH = '.upp.config'
|
|
9
|
-
|
|
10
|
-
__CONFIG_PATH_BAK = '_upp.config'
|
|
11
7
|
|
|
12
8
|
# 创建一个线程本地存储对象
|
|
13
9
|
__THREAD_LOCAL_INDEX_DATA = threading.local()
|
|
@@ -566,70 +562,6 @@ def format_sorted_kv_string(data_obj: Any,
|
|
|
566
562
|
return sep.join(result)
|
|
567
563
|
|
|
568
564
|
|
|
569
|
-
# 将数据写入到 config 中
|
|
570
|
-
def set_config_data(file_name: str = 'config',
|
|
571
|
-
param: dict[str, Any] = None) -> None:
|
|
572
|
-
set_data_in_user_home(file_name, {} if param is None else param)
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
# 从 config 中获得 配置数据
|
|
576
|
-
def get_config_data(file_name: str = 'config') -> dict[str, Any]:
|
|
577
|
-
# print('get_config_data', file_name)
|
|
578
|
-
config_data = get_data_from_user_home(file_name)
|
|
579
|
-
# print('get_data_from_user_home', config_data)
|
|
580
|
-
if not config_data:
|
|
581
|
-
config_data = get_data_from_path(file_name)
|
|
582
|
-
# print('get_data_from_path', config_data)
|
|
583
|
-
return config_data
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
# 在当前用户的主目录中, 获得指定文件的数据
|
|
587
|
-
def get_data_from_user_home(file_name: str = 'config') -> dict[str, Any]:
|
|
588
|
-
return get_data_from_path(file_name, os.path.expanduser("~"))
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
# 将 data 数据,在当前用户的主目录中, 获得指定文件的数据
|
|
592
|
-
def set_data_in_user_home(file_name: str = 'config',
|
|
593
|
-
param: dict[str, Any] = None) -> None:
|
|
594
|
-
set_data_in_path(file_name, {} if param is None else param, os.path.expanduser("~"))
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
# 在当前的目录中, 获得指定文件的数据
|
|
598
|
-
def get_data_from_path(file_name: str = 'config',
|
|
599
|
-
file_path: str = None) -> dict[str, Any]:
|
|
600
|
-
param = get_data_from_path_detail(file_name, file_path, __CONFIG_PATH)
|
|
601
|
-
return param if param else get_data_from_path_detail(file_name, file_path, __CONFIG_PATH_BAK)
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
def get_data_from_path_detail(file_name: str = 'config',
|
|
605
|
-
file_path: str = None,
|
|
606
|
-
path_name: str = __CONFIG_PATH) -> dict[str, Any]:
|
|
607
|
-
config_path = file_path + '/' + path_name if file_path else path_name
|
|
608
|
-
# print('config_path_1', config_path)
|
|
609
|
-
if not os.path.exists(config_path):
|
|
610
|
-
# print('config_path_2', config_path)
|
|
611
|
-
return {}
|
|
612
|
-
file_path = config_path + '/' + file_name + '.json'
|
|
613
|
-
# print('config_path_3', file_path)
|
|
614
|
-
if not os.path.exists(file_path):
|
|
615
|
-
return {}
|
|
616
|
-
# print('to_json_from_file', file_path)
|
|
617
|
-
return to_json_from_file(file_path)
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
# 在当前的目录中, 设置数据到指定路径下
|
|
621
|
-
def set_data_in_path(file_name: str = 'config',
|
|
622
|
-
param: str = None,
|
|
623
|
-
file_path: str = '') -> None:
|
|
624
|
-
config_path = file_path + '/' + __CONFIG_PATH
|
|
625
|
-
if not os.path.exists(config_path):
|
|
626
|
-
os.mkdir(config_path)
|
|
627
|
-
file_path = config_path + '/' + file_name + '.json'
|
|
628
|
-
text_file = open(file_path, 'w', encoding='utf-8')
|
|
629
|
-
text_file.write(to_str({} if param is None else param))
|
|
630
|
-
text_file.close()
|
|
631
|
-
|
|
632
|
-
|
|
633
565
|
# 执行命令
|
|
634
566
|
def exec_command(command: str = None) -> None:
|
|
635
567
|
if command is None:
|
|
@@ -640,40 +572,6 @@ def exec_command(command: str = None) -> None:
|
|
|
640
572
|
print(f"command executed error: {e}")
|
|
641
573
|
|
|
642
574
|
|
|
643
|
-
# 找到最新的 html 文件
|
|
644
|
-
def get_latest_file(
|
|
645
|
-
file_path: str = None,
|
|
646
|
-
path_prefix: str = None,
|
|
647
|
-
prefix: str = None,
|
|
648
|
-
path_contain: str = None,
|
|
649
|
-
contain: str = None,
|
|
650
|
-
path_suffix: str = None,
|
|
651
|
-
suffix: str = None,
|
|
652
|
-
full_path: bool = None) -> None | tuple[str, str] | str:
|
|
653
|
-
"""
|
|
654
|
-
full_path: 是否返回完整的路径
|
|
655
|
-
"""
|
|
656
|
-
html_list = get_file(file_path=file_path,
|
|
657
|
-
path_prefix=path_prefix,
|
|
658
|
-
prefix=prefix,
|
|
659
|
-
path_contain=path_contain,
|
|
660
|
-
contain=contain,
|
|
661
|
-
path_suffix=path_suffix,
|
|
662
|
-
suffix=suffix)
|
|
663
|
-
html_list.sort(reverse=True)
|
|
664
|
-
r1 = html_list[0] if len(html_list) > 0 else None
|
|
665
|
-
if r1 is None:
|
|
666
|
-
return None
|
|
667
|
-
r1_short = r1
|
|
668
|
-
for sep in ['\\', '/']:
|
|
669
|
-
if sep in r1:
|
|
670
|
-
r1_short = r1.split(sep)[-1]
|
|
671
|
-
break
|
|
672
|
-
if full_path is None:
|
|
673
|
-
return r1_short, r1
|
|
674
|
-
return r1 if full_path else r1_short
|
|
675
|
-
|
|
676
|
-
|
|
677
575
|
# 是否是 windows 系统
|
|
678
576
|
def is_win() -> bool:
|
|
679
577
|
return platform.system().lower() == 'windows'
|
|
@@ -684,47 +582,6 @@ def is_linux() -> bool:
|
|
|
684
582
|
return not is_win()
|
|
685
583
|
|
|
686
584
|
|
|
687
|
-
def to_print(*args,
|
|
688
|
-
time_prefix: bool = False,
|
|
689
|
-
line_with_space_count: int = None,
|
|
690
|
-
interval: int = None) -> str:
|
|
691
|
-
"""
|
|
692
|
-
记录日志, 如果是对象会转化为 json
|
|
693
|
-
数据直接 print, 不记录到文件
|
|
694
|
-
例如: aaa
|
|
695
|
-
interval: 间隔一段时间,打印一下, 单位: 秒,不要频繁打印
|
|
696
|
-
time_prefix : 是否在前面加时间, 默认 False
|
|
697
|
-
"""
|
|
698
|
-
d = ' '.join(map(lambda x: json.dumps(x) if is_json_serializable(x) else str(x), args))
|
|
699
|
-
d = d.strip()
|
|
700
|
-
lo = datetime.today().strftime('%Y-%m-%d %H:%M:%S') + ' ' + d if time_prefix is True else d
|
|
701
|
-
if lo is None or str(lo) == '':
|
|
702
|
-
lo = to_datetime(r_str=True)
|
|
703
|
-
prefix_space = ' ' * (line_with_space_count or 0)
|
|
704
|
-
if interval is None or get_timestamp() - get_thread_local_index_data().get('to_print_time', 0) >= interval:
|
|
705
|
-
get_thread_local_index_data()['to_print_time'] = get_timestamp()
|
|
706
|
-
s = ''
|
|
707
|
-
if interval is not None:
|
|
708
|
-
s = str(to_datetime()) + ' '
|
|
709
|
-
print(prefix_space + s + str(lo))
|
|
710
|
-
return prefix_space + lo
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
def to_log(*args,
|
|
714
|
-
time_prefix: bool = False,
|
|
715
|
-
line_with_space_count: int = None,
|
|
716
|
-
interval: int = None) -> str:
|
|
717
|
-
"""
|
|
718
|
-
记录日志, 如果是对象会转化为 json
|
|
719
|
-
前面加了时间
|
|
720
|
-
例如: 2024-11-07 10:23:47 aaa
|
|
721
|
-
"""
|
|
722
|
-
return to_print(*args,
|
|
723
|
-
time_prefix=time_prefix if time_prefix is not None else True,
|
|
724
|
-
line_with_space_count=line_with_space_count,
|
|
725
|
-
interval=interval)
|
|
726
|
-
|
|
727
|
-
|
|
728
585
|
def list_group_by(data_list: list[list],
|
|
729
586
|
group_by_index: int = 0) -> list[list[list]]:
|
|
730
587
|
"""
|
|
@@ -797,171 +654,6 @@ def get_and_save_data_to_thread(file_path: str = None,
|
|
|
797
654
|
return r_list
|
|
798
655
|
|
|
799
656
|
|
|
800
|
-
def to_print_file(*args,
|
|
801
|
-
file_path: str = None,
|
|
802
|
-
file_name: str = None,
|
|
803
|
-
file_name_with_date: bool = False,
|
|
804
|
-
file_name_prefix: str = None,
|
|
805
|
-
file_name_suffix: str = None,
|
|
806
|
-
line_with_space_count: int = None,
|
|
807
|
-
mode: str = 'a',
|
|
808
|
-
interval: int = None) -> str:
|
|
809
|
-
"""
|
|
810
|
-
将 print 数据, 写入到 print_file 文件
|
|
811
|
-
文件按照 日期自动创建
|
|
812
|
-
例如: print_file/2020-01-01.txt
|
|
813
|
-
to_print_file(query_string, mode='w', file_path=file_path, file_name=get_file_name(file_name=file_path, is_date=True))
|
|
814
|
-
"""
|
|
815
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
816
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
817
|
-
file_path=file_path,
|
|
818
|
-
file_name=file_name,
|
|
819
|
-
file_name_prefix=file_name_prefix,
|
|
820
|
-
file_name_suffix=file_name_suffix,
|
|
821
|
-
interval=interval,
|
|
822
|
-
mode=mode,
|
|
823
|
-
file_name_with_date=file_name_with_date,
|
|
824
|
-
line_with_space_count=line_with_space_count,
|
|
825
|
-
fun_name=to_print_file
|
|
826
|
-
)
|
|
827
|
-
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
828
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
829
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
830
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
831
|
-
file_path=str(file_path if file_path is not None else 'to_print_file'),
|
|
832
|
-
mode=mode,
|
|
833
|
-
fixed_name=True,
|
|
834
|
-
suffix='.txt')
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
def to_print_txt(*args,
|
|
838
|
-
file_path: str = None,
|
|
839
|
-
file_name_with_date: bool = False,
|
|
840
|
-
file_name: str = None,
|
|
841
|
-
file_name_prefix: str = None,
|
|
842
|
-
file_name_suffix: str = None,
|
|
843
|
-
line_with_space_count: int = None,
|
|
844
|
-
mode: str = 'a',
|
|
845
|
-
interval: int = None) -> str:
|
|
846
|
-
"""
|
|
847
|
-
将 print 数据, 写入到 print_txt 文件
|
|
848
|
-
文件按照 日期自动创建
|
|
849
|
-
例如: print_txt/2020-01-01.txt
|
|
850
|
-
"""
|
|
851
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
852
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
853
|
-
file_path=file_path,
|
|
854
|
-
file_name=file_name,
|
|
855
|
-
file_name_prefix=file_name_prefix,
|
|
856
|
-
file_name_suffix=file_name_suffix,
|
|
857
|
-
interval=interval,
|
|
858
|
-
mode=mode,
|
|
859
|
-
file_name_with_date=file_name_with_date,
|
|
860
|
-
line_with_space_count=line_with_space_count,
|
|
861
|
-
fun_name=to_print_txt
|
|
862
|
-
)
|
|
863
|
-
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
864
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
865
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
866
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
867
|
-
file_path=str(file_path if file_path is not None else 'to_print_txt'),
|
|
868
|
-
mode=mode,
|
|
869
|
-
fixed_name=True,
|
|
870
|
-
suffix='.txt')
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
def to_log_file(*args,
|
|
874
|
-
file_path: str = None,
|
|
875
|
-
file_name_with_date: bool = False,
|
|
876
|
-
file_name: str = None,
|
|
877
|
-
file_name_prefix: str = None,
|
|
878
|
-
file_name_suffix: str = None,
|
|
879
|
-
line_with_space_count: int = None,
|
|
880
|
-
time_prefix: bool = True,
|
|
881
|
-
mode: str = 'a',
|
|
882
|
-
interval: int = None) -> None:
|
|
883
|
-
"""
|
|
884
|
-
将 log 数据, 写入到 log_file 文件
|
|
885
|
-
文件按照 日期自动创建
|
|
886
|
-
例如: log_file/2020-01-01.log
|
|
887
|
-
"""
|
|
888
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
889
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
890
|
-
file_path=file_path,
|
|
891
|
-
file_name=file_name,
|
|
892
|
-
file_name_prefix=file_name_prefix,
|
|
893
|
-
file_name_suffix=file_name_suffix,
|
|
894
|
-
interval=interval,
|
|
895
|
-
mode=mode,
|
|
896
|
-
file_name_with_date=file_name_with_date,
|
|
897
|
-
line_with_space_count=line_with_space_count,
|
|
898
|
-
fun_name=to_log_file
|
|
899
|
-
)
|
|
900
|
-
to_txt(data_param=[
|
|
901
|
-
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
902
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
903
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
904
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
905
|
-
file_path=str(file_path if file_path is not None else 'to_log_file'),
|
|
906
|
-
fixed_name=True,
|
|
907
|
-
mode=mode,
|
|
908
|
-
suffix='.log')
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
def to_log_txt(*args,
|
|
912
|
-
file_path: str = None,
|
|
913
|
-
file_name_with_date: bool = False,
|
|
914
|
-
file_name: str = None,
|
|
915
|
-
file_name_prefix: str = None,
|
|
916
|
-
file_name_suffix: str = None,
|
|
917
|
-
line_with_space_count: int = None,
|
|
918
|
-
time_prefix: bool = True,
|
|
919
|
-
mode: str = 'a',
|
|
920
|
-
interval: int = None) -> None:
|
|
921
|
-
"""
|
|
922
|
-
将 log 数据, 写入到 log_txt 文件夹中
|
|
923
|
-
文件按照 日期自动创建
|
|
924
|
-
例如: log_txt/2020-01-01.txt
|
|
925
|
-
"""
|
|
926
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
927
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
928
|
-
file_path=file_path,
|
|
929
|
-
file_name=file_name,
|
|
930
|
-
file_name_prefix=file_name_prefix,
|
|
931
|
-
file_name_suffix=file_name_suffix,
|
|
932
|
-
interval=interval,
|
|
933
|
-
mode=mode,
|
|
934
|
-
file_name_with_date=file_name_with_date,
|
|
935
|
-
line_with_space_count=line_with_space_count,
|
|
936
|
-
fun_name=to_log_txt
|
|
937
|
-
)
|
|
938
|
-
to_txt(data_param=[
|
|
939
|
-
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
940
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
941
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
942
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
943
|
-
file_path=str(file_path if file_path is not None else 'to_log_txt'),
|
|
944
|
-
mode=mode,
|
|
945
|
-
fixed_name=True,
|
|
946
|
-
suffix='.txt')
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
def check_file(file_name: str = None) -> None:
|
|
950
|
-
r"""
|
|
951
|
-
检查文件夹是否存在,不存在,就创建新的
|
|
952
|
-
支持多级目录 , 例如: C:\Users\yangpu\Desktop\study\a\b\c\d\e\f
|
|
953
|
-
"""
|
|
954
|
-
if file_name is None or file_name == '':
|
|
955
|
-
return
|
|
956
|
-
for sep in ['\\', '/']:
|
|
957
|
-
f_n = file_name.split(sep)
|
|
958
|
-
for i in range(1, len(f_n) + 1):
|
|
959
|
-
# C:\Users\yangpu\Desktop\study\p.t
|
|
960
|
-
p_n = sep.join(f_n[0:i])
|
|
961
|
-
if p_n and not os.path.exists(p_n):
|
|
962
|
-
os.mkdir(p_n)
|
|
963
|
-
|
|
964
|
-
|
|
965
657
|
def get_file_name(file_name: str = None,
|
|
966
658
|
suffix: str = '.txt',
|
|
967
659
|
is_date: bool = False) -> str:
|
|
@@ -981,308 +673,6 @@ def get_file_name(file_name: str = None,
|
|
|
981
673
|
return str(file_name) + '_' + s + '_' + second + random_int_str(length=2) + suffix
|
|
982
674
|
|
|
983
675
|
|
|
984
|
-
def to_txt(data_param: Any,
|
|
985
|
-
file_name: str = 'txt',
|
|
986
|
-
file_path: str = 'txt',
|
|
987
|
-
fixed_name: bool = False,
|
|
988
|
-
mode: str = 'a',
|
|
989
|
-
suffix: str = '.txt',
|
|
990
|
-
sep_list: str = '\t',
|
|
991
|
-
file_name_is_date: bool = False) -> str:
|
|
992
|
-
r"""
|
|
993
|
-
将 list 中的数据以 json 或者基本类型的形式写入到文件中
|
|
994
|
-
data_param : 数组数据, 也可以不是数组
|
|
995
|
-
file_name : 文件名 , 默认 txt
|
|
996
|
-
当文件名是 C:\Users\yangpu\Desktop\study\abc\d\e\f\a.sql 这种类型的时候, 可以直接创建文件夹,
|
|
997
|
-
会赋值 file_name=a,
|
|
998
|
-
file_path=C:\Users\yangpu\Desktop\study\abc\d\e\f,
|
|
999
|
-
fixed_name=True,
|
|
1000
|
-
suffix=.sql
|
|
1001
|
-
当文件名是 abc 的时候, 按照正常值,计算
|
|
1002
|
-
file_path : 文件路径
|
|
1003
|
-
fixed_name : 是否固定文件名
|
|
1004
|
-
suffix : 文件后缀, 默认 .txt
|
|
1005
|
-
sep_list : 当 data_param 是 list(list) 类型的时候 使用 sep_list 作为分割内部的分隔符,
|
|
1006
|
-
默认使用 \t 作为分隔符, 如果为 None , 则按照 json 去处理这个 list
|
|
1007
|
-
"""
|
|
1008
|
-
file_name = str(file_name)
|
|
1009
|
-
for sep in ['\\', '/']:
|
|
1010
|
-
f_n = file_name.split(sep)
|
|
1011
|
-
if len(f_n) > 1:
|
|
1012
|
-
file_name = f_n[-1]
|
|
1013
|
-
file_path = sep.join(f_n[0:-1])
|
|
1014
|
-
if '.' in file_name:
|
|
1015
|
-
suffix = '.' + file_name.split('.')[-1]
|
|
1016
|
-
file_name = file_name[0:file_name.rfind('.')]
|
|
1017
|
-
fixed_name = True
|
|
1018
|
-
|
|
1019
|
-
# 检查路径 file_path
|
|
1020
|
-
while file_path.endswith('/'):
|
|
1021
|
-
file_path = file_path[0:-1]
|
|
1022
|
-
check_file(file_path)
|
|
1023
|
-
|
|
1024
|
-
# 在 file_name 中, 检查是否有后缀
|
|
1025
|
-
if '.' in file_name:
|
|
1026
|
-
suffix = '.' + file_name.split('.')[-1]
|
|
1027
|
-
file_name = file_name[0:file_name.rfind('.')]
|
|
1028
|
-
|
|
1029
|
-
# 生成 file_name
|
|
1030
|
-
if fixed_name:
|
|
1031
|
-
file_name = file_name + suffix
|
|
1032
|
-
else:
|
|
1033
|
-
file_name = get_file_name(file_name, suffix, is_date=file_name_is_date)
|
|
1034
|
-
# 文件路径
|
|
1035
|
-
file_name_path = file_name
|
|
1036
|
-
if file_path != '':
|
|
1037
|
-
file_name_path = file_path + '/' + file_name
|
|
1038
|
-
# 写入文件
|
|
1039
|
-
text_file = open(file_name_path, mode, encoding='utf-8')
|
|
1040
|
-
if isinstance(data_param, set):
|
|
1041
|
-
data_param = list(data_param)
|
|
1042
|
-
if not isinstance(data_param, list):
|
|
1043
|
-
text_file.write(to_str(data_param) + '\n')
|
|
1044
|
-
else:
|
|
1045
|
-
for one in data_param:
|
|
1046
|
-
if isinstance(one, (list, tuple, set)) and sep_list is not None:
|
|
1047
|
-
text_file.write(str(sep_list).join(list(map(lambda x: to_str(x), one))) + '\n')
|
|
1048
|
-
else:
|
|
1049
|
-
text_file.write(to_str(one) + '\n')
|
|
1050
|
-
text_file.close()
|
|
1051
|
-
return file_name_path
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
1055
|
-
def to_txt_file(data_param: Any,
|
|
1056
|
-
file_name: str = None,
|
|
1057
|
-
mode: str = 'a') -> str:
|
|
1058
|
-
file_name = get_file_name('to_txt_file', is_date=True) if file_name is None else file_name
|
|
1059
|
-
suffix = '.txt'
|
|
1060
|
-
f = file_name
|
|
1061
|
-
for sep in ['\\', '/']:
|
|
1062
|
-
f_n = file_name.split(sep)
|
|
1063
|
-
if len(f_n) > 1:
|
|
1064
|
-
f = file_name
|
|
1065
|
-
if '.' in f:
|
|
1066
|
-
suffix = '.' + f.split('.')[-1]
|
|
1067
|
-
file_name = file_name.replace(suffix, '')
|
|
1068
|
-
return to_txt(data_param=data_param,
|
|
1069
|
-
file_name=file_name,
|
|
1070
|
-
file_path='to_txt_file',
|
|
1071
|
-
suffix=suffix,
|
|
1072
|
-
fixed_name=True,
|
|
1073
|
-
mode=mode)
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
1077
|
-
def to_file(data_param: Any,
|
|
1078
|
-
file_name: str = None,
|
|
1079
|
-
mode: str = 'a') -> str:
|
|
1080
|
-
file_name = get_file_name('to_file', is_date=True) if file_name is None else file_name
|
|
1081
|
-
suffix = '.txt'
|
|
1082
|
-
f = file_name
|
|
1083
|
-
for sep in ['\\', '/']:
|
|
1084
|
-
f_n = file_name.split(sep)
|
|
1085
|
-
if len(f_n) > 1:
|
|
1086
|
-
f = file_name
|
|
1087
|
-
if '.' in f:
|
|
1088
|
-
suffix = '.' + f.split('.')[-1]
|
|
1089
|
-
file_name = file_name.replace(suffix, '')
|
|
1090
|
-
return to_txt(data_param=data_param,
|
|
1091
|
-
file_name=file_name,
|
|
1092
|
-
file_path='file',
|
|
1093
|
-
suffix=suffix,
|
|
1094
|
-
fixed_name=True,
|
|
1095
|
-
mode=mode)
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
def to_list(file_name: str = 'a.txt',
|
|
1099
|
-
sep: str = None,
|
|
1100
|
-
sep_line: str = None,
|
|
1101
|
-
sep_line_contain: str = None,
|
|
1102
|
-
sep_line_prefix: str = None,
|
|
1103
|
-
sep_line_suffix: str = None,
|
|
1104
|
-
sep_all: str = None,
|
|
1105
|
-
ignore_start_with: list[str] | set[str] | str = None,
|
|
1106
|
-
ignore_end_with: list[str] | set[str] | str | None = None,
|
|
1107
|
-
start_index: int = None,
|
|
1108
|
-
start_line: str = None,
|
|
1109
|
-
end_index: int = None,
|
|
1110
|
-
end_line: str = None,
|
|
1111
|
-
count: int = None,
|
|
1112
|
-
sheet_index: int = 1,
|
|
1113
|
-
column_index: list[str] | set[str] | str | None = None,
|
|
1114
|
-
column_date: list[str] | set[str] | str | None = None,
|
|
1115
|
-
column_datetime: list[str] | set[str] | str | None = None) -> list:
|
|
1116
|
-
"""
|
|
1117
|
-
当读取 txt 之类的文件的时候
|
|
1118
|
-
将 txt 文件读取到 list 中, 每一行自动过滤掉行前行后的特殊字符
|
|
1119
|
-
sep : 是否对每一行进行分割,如果存在这个字段,就分割
|
|
1120
|
-
sep_all : 将文件转化成一个字符串,然后对这个字符串,再次总体分割
|
|
1121
|
-
start_index : 从这个地方开始读取,从1开始标号 , 包含这一行
|
|
1122
|
-
start_line : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 包含这一行
|
|
1123
|
-
end_index : 读取到这个地方结束,从1开始标号 , 不包含这一行
|
|
1124
|
-
end_line : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行
|
|
1125
|
-
count : 读取指定的行数
|
|
1126
|
-
##############################################
|
|
1127
|
-
当读取 excel 之类的文件的时候
|
|
1128
|
-
将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
|
|
1129
|
-
sheet : 从 1 开始编号,
|
|
1130
|
-
column_index : 从 1 开始编号, 指定列
|
|
1131
|
-
column_index : 如果是指定值, 这个时候返回的是一个 list, 没有嵌套 list
|
|
1132
|
-
column_index : 如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
|
|
1133
|
-
column_date : 指定日期格式的列,规则与 column_index 一样
|
|
1134
|
-
column_datetime : 指定日期格式的列,规则与 column_index 一样
|
|
1135
|
-
返回的数据一定是一个 list
|
|
1136
|
-
"""
|
|
1137
|
-
if file_name.endswith('.xls') or file_name.endswith('.xlsx'):
|
|
1138
|
-
return to_list_from_excel(file_name=file_name,
|
|
1139
|
-
sheet_index=sheet_index,
|
|
1140
|
-
column_index=column_index,
|
|
1141
|
-
column_date=column_date,
|
|
1142
|
-
column_datetime=column_datetime)
|
|
1143
|
-
return to_list_from_txt(file_name=file_name,
|
|
1144
|
-
sep=sep,
|
|
1145
|
-
sep_line=sep_line,
|
|
1146
|
-
sep_line_contain=sep_line_contain,
|
|
1147
|
-
sep_line_prefix=sep_line_prefix,
|
|
1148
|
-
sep_line_suffix=sep_line_suffix,
|
|
1149
|
-
sep_all=sep_all,
|
|
1150
|
-
ignore_start_with=ignore_start_with,
|
|
1151
|
-
ignore_end_with=ignore_end_with,
|
|
1152
|
-
start_index=start_index,
|
|
1153
|
-
start_line=start_line,
|
|
1154
|
-
end_index=end_index,
|
|
1155
|
-
end_line=end_line,
|
|
1156
|
-
count=count)
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
def to_list_from_excel(file_name: str = 'a.xls',
|
|
1160
|
-
sheet_index: int = 1,
|
|
1161
|
-
column_index: list | int | str | None = None,
|
|
1162
|
-
column_date: list | int | str | None = None,
|
|
1163
|
-
column_datetime: list | int | str | None = None) -> list:
|
|
1164
|
-
"""
|
|
1165
|
-
当读取 excel 之类的文件的时候
|
|
1166
|
-
将 excel 文件读取到 list 中, 可以指定 sheet, 也可以指定列 column_index(列) ,自动过滤掉每个单元格前后的特殊字符
|
|
1167
|
-
sheet_index : 从 1 开始编号,
|
|
1168
|
-
column_index : 从 1 开始编号, 指定列, 如果是指定值是一个, 这个时候返回的是一个 list, 没有嵌套 list
|
|
1169
|
-
如果是 '1,2,3,4' [1,2,3,4], 返回的是一个嵌套 list[list]
|
|
1170
|
-
column_date : 指定日期格式的列,规则与 column_index 一样
|
|
1171
|
-
column_datetime : 指定日期格式的列,规则与 column_index 一样
|
|
1172
|
-
"""
|
|
1173
|
-
if file_is_empty(file_name):
|
|
1174
|
-
return []
|
|
1175
|
-
data_list = list()
|
|
1176
|
-
# excel 表格解析成 list 数据
|
|
1177
|
-
list_index = []
|
|
1178
|
-
for one_index in [column_index, column_date, column_datetime]:
|
|
1179
|
-
list_index_one = None
|
|
1180
|
-
if one_index is not None:
|
|
1181
|
-
list_index_one = []
|
|
1182
|
-
if isinstance(one_index, int):
|
|
1183
|
-
list_index_one.append(one_index)
|
|
1184
|
-
if isinstance(one_index, str):
|
|
1185
|
-
i_list = one_index.split(',')
|
|
1186
|
-
for i in i_list:
|
|
1187
|
-
list_index_one.append(int(i))
|
|
1188
|
-
if isinstance(one_index, list):
|
|
1189
|
-
for i in one_index:
|
|
1190
|
-
list_index_one.append(int(i))
|
|
1191
|
-
list_index.append(list_index_one)
|
|
1192
|
-
list_all = []
|
|
1193
|
-
for one_list in list_index:
|
|
1194
|
-
if one_list is not None:
|
|
1195
|
-
for o in one_list:
|
|
1196
|
-
list_all.append(o)
|
|
1197
|
-
if len(list_all) > 0 and list_index[0] is not None:
|
|
1198
|
-
list_index[0] = list_all
|
|
1199
|
-
# 是否是单 list 类型的数据
|
|
1200
|
-
list_only_one = False
|
|
1201
|
-
if list_index[0] is not None and len(list_index[0]) == 1:
|
|
1202
|
-
list_only_one = True
|
|
1203
|
-
# 是 xls 格式
|
|
1204
|
-
if file_name.endswith('.xls'):
|
|
1205
|
-
book = xlrd.open_workbook(file_name) # 打开一个excel
|
|
1206
|
-
sheet = book.sheet_by_index(sheet_index - 1) # 根据顺序获取sheet
|
|
1207
|
-
for i in range(sheet.nrows): # 0 1 2 3 4 5
|
|
1208
|
-
rows = sheet.row_values(i)
|
|
1209
|
-
row_data = []
|
|
1210
|
-
for j in range(len(rows)):
|
|
1211
|
-
cell_data = str(rows[j]).strip()
|
|
1212
|
-
is_date = False
|
|
1213
|
-
is_datetime = False
|
|
1214
|
-
# 日期格式的列
|
|
1215
|
-
if list_index[1] is not None and j + 1 in list_index[1]:
|
|
1216
|
-
cell_data = to_date(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
|
|
1217
|
-
is_date = True
|
|
1218
|
-
row_data.append(cell_data)
|
|
1219
|
-
if list_only_one:
|
|
1220
|
-
row_data = cell_data
|
|
1221
|
-
# 日期时间格式的列
|
|
1222
|
-
if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
|
|
1223
|
-
cell_data = to_datetime(xlrd.xldate_as_datetime(to_int(rows[j]), 0))
|
|
1224
|
-
is_datetime = True
|
|
1225
|
-
row_data.append(cell_data)
|
|
1226
|
-
if list_only_one:
|
|
1227
|
-
row_data = cell_data
|
|
1228
|
-
# 指定需要的列
|
|
1229
|
-
if not is_date and not is_datetime:
|
|
1230
|
-
if list_index[0] is None:
|
|
1231
|
-
row_data.append(cell_data)
|
|
1232
|
-
else:
|
|
1233
|
-
# 指定需要的列
|
|
1234
|
-
if j + 1 in list_index[0]:
|
|
1235
|
-
row_data.append(cell_data)
|
|
1236
|
-
if list_only_one:
|
|
1237
|
-
row_data = cell_data
|
|
1238
|
-
data_list.append(row_data)
|
|
1239
|
-
# 是 xlsx 格式
|
|
1240
|
-
if file_name.endswith('.xlsx'):
|
|
1241
|
-
wb = openpyxl.load_workbook(filename=file_name, read_only=True)
|
|
1242
|
-
ws = wb[wb.sheetnames[sheet_index - 1]]
|
|
1243
|
-
for rows in ws.rows:
|
|
1244
|
-
row_data = []
|
|
1245
|
-
for j in range(len(rows)):
|
|
1246
|
-
cell_data = str(rows[j].value).strip()
|
|
1247
|
-
is_date = False
|
|
1248
|
-
is_datetime = False
|
|
1249
|
-
# 日期格式的列
|
|
1250
|
-
if list_index[1] is not None and j + 1 in list_index[1]:
|
|
1251
|
-
cell_data = to_date(cell_data)
|
|
1252
|
-
is_date = True
|
|
1253
|
-
row_data.append(cell_data)
|
|
1254
|
-
if list_only_one:
|
|
1255
|
-
row_data = cell_data
|
|
1256
|
-
# 日期时间格式的列
|
|
1257
|
-
if not is_date and list_index[2] is not None and j + 1 in list_index[2]:
|
|
1258
|
-
cell_data = to_datetime(cell_data)
|
|
1259
|
-
is_datetime = True
|
|
1260
|
-
row_data.append(cell_data)
|
|
1261
|
-
if list_only_one:
|
|
1262
|
-
row_data = cell_data
|
|
1263
|
-
# 指定需要的列
|
|
1264
|
-
if not is_date and not is_datetime:
|
|
1265
|
-
if list_index[0] is None:
|
|
1266
|
-
row_data.append(cell_data)
|
|
1267
|
-
else:
|
|
1268
|
-
# 指定需要的列
|
|
1269
|
-
if j + 1 in list_index[0]:
|
|
1270
|
-
row_data.append(cell_data)
|
|
1271
|
-
if list_only_one:
|
|
1272
|
-
row_data = cell_data
|
|
1273
|
-
data_list.append(row_data)
|
|
1274
|
-
return data_list
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
def to_list_from_txt_with_blank_line(file_name: str = 'a.txt') -> list:
|
|
1278
|
-
"""
|
|
1279
|
-
将一个文件中以空行作为分隔符,
|
|
1280
|
-
组成一个 list(list) 数据
|
|
1281
|
-
多行空行,自动合并到一行空行
|
|
1282
|
-
"""
|
|
1283
|
-
return to_list_from_txt(file_name, sep_line='')
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
676
|
def to_list_list(data_list: list = None,
|
|
1287
677
|
count: int = 10) -> list:
|
|
1288
678
|
"""
|
|
@@ -1307,34 +697,6 @@ def to_list_list(data_list: list = None,
|
|
|
1307
697
|
return r_list
|
|
1308
698
|
|
|
1309
699
|
|
|
1310
|
-
def to_list_json_from_txt(file_name: str = 'a.txt',
|
|
1311
|
-
start_index: int = None,
|
|
1312
|
-
start_line: str = None,
|
|
1313
|
-
start_line_exclude: str | list[str] | set[str] = None,
|
|
1314
|
-
end_index: int = None,
|
|
1315
|
-
end_line: str = None,
|
|
1316
|
-
end_line_exclude: str | list[str] | set[str] = None,
|
|
1317
|
-
count: int = None) -> list:
|
|
1318
|
-
"""
|
|
1319
|
-
将一个文件中的数据按照行来区分,
|
|
1320
|
-
会自动过滤掉空格行,
|
|
1321
|
-
组成一个 list[json] 数据
|
|
1322
|
-
可以将以下文本转 list[json]
|
|
1323
|
-
{"accessKey":"1","signature":"4","timestamp":"1747639787"}
|
|
1324
|
-
{"accessKey":"2","signature":"5","timestamp":"1747639787"}
|
|
1325
|
-
{"accessKey":"3","signature":"6","timestamp":"1747639787"}
|
|
1326
|
-
"""
|
|
1327
|
-
return to_list_from_txt(file_name,
|
|
1328
|
-
start_index=start_index,
|
|
1329
|
-
start_line=start_line,
|
|
1330
|
-
start_line_exclude=start_line_exclude,
|
|
1331
|
-
end_index=end_index,
|
|
1332
|
-
end_line=end_line,
|
|
1333
|
-
end_line_exclude=end_line_exclude,
|
|
1334
|
-
count=count,
|
|
1335
|
-
line_json=True)
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
700
|
# 将多个文件 读取成 list
|
|
1339
701
|
def to_list_from_txt_list(file_list: list[Any] = None) -> list:
|
|
1340
702
|
if file_list is None:
|
|
@@ -1343,396 +705,3 @@ def to_list_from_txt_list(file_list: list[Any] = None) -> list:
|
|
|
1343
705
|
for a in file_list:
|
|
1344
706
|
data_list.extend(to_list_from_txt(file_name=a))
|
|
1345
707
|
return data_list
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
def to_list_from_txt(file_name: str = 'a.txt',
|
|
1349
|
-
sep: str = None,
|
|
1350
|
-
sep_line: str = None,
|
|
1351
|
-
sep_line_contain: str = None,
|
|
1352
|
-
sep_line_prefix: str = None,
|
|
1353
|
-
sep_line_suffix: str = None,
|
|
1354
|
-
sep_line_with_space_count: int = None,
|
|
1355
|
-
sep_is_front: bool = True,
|
|
1356
|
-
sep_all: str = None,
|
|
1357
|
-
ignore_start_with: list | int | str | None = None,
|
|
1358
|
-
ignore_end_with: list | int | str | None = None,
|
|
1359
|
-
ignore_empty: bool | None = None,
|
|
1360
|
-
line_join: str = None,
|
|
1361
|
-
line_json: bool = None,
|
|
1362
|
-
start_index: int = None,
|
|
1363
|
-
start_line: str = None,
|
|
1364
|
-
start_line_exclude: str | list[str] | set[str] = None,
|
|
1365
|
-
end_index: int = None,
|
|
1366
|
-
end_line: str = None,
|
|
1367
|
-
end_line_exclude: str | list[str] | set[str] = None,
|
|
1368
|
-
count: int = None) -> list:
|
|
1369
|
-
"""
|
|
1370
|
-
将 txt 文件转化成 list 的方法
|
|
1371
|
-
当读取 txt 之类的文件的时候
|
|
1372
|
-
将 txt 文件读取到 list 中, 每一行自动过滤掉行前行后的特殊字符
|
|
1373
|
-
sep : 对每一行进行分割,将 list(str) 转化为 list(list(str)), 或者将 list(list(str)) 转化为 list(list(list(str)))
|
|
1374
|
-
sep_line : 这一行是一个分隔符, 分隔符与这行一样, 将 list(str) 转化为 list(list(str))
|
|
1375
|
-
sep_line_with_space_count : 分隔符是空格的个数, 将 list(str) 转化为 list(list(str))
|
|
1376
|
-
sep_line_contain : 这一行是一个分隔符,包含这个行分隔符的做分割, 将 list(str) 转化为 list(list(str))
|
|
1377
|
-
sep_line_prefix : 这一行是一个分隔符,以这个分隔符作为前缀的, 将 list(str) 转化为 list(list(str))
|
|
1378
|
-
sep_line_suffix : 这一行是一个分隔符,以这个分隔符作为后缀的, 将 list(str) 转化为 list(list(str))
|
|
1379
|
-
sep_is_front : 这一行,分割行,是包含到前面,还是包含到
|
|
1380
|
-
sep_all : 将文件转化成一个字符串,然后对这个字符串,再次总体分割 将 list(str) 转化为 str , 然后再次转化成 list(str)
|
|
1381
|
-
ignore_start_with : 忽略以这个为开头的行
|
|
1382
|
-
ignore_end_with : 忽略以这个为结尾的行
|
|
1383
|
-
ignore_empty : 如果这一行为空,就忽略这行
|
|
1384
|
-
line_join : 将 list(list(str)) 转化成 list(str) 类型的数据
|
|
1385
|
-
line_json : 将 list(str) 转化成 list(json) 类型的数据, 会自动过滤掉空格行
|
|
1386
|
-
start_index : 从这个地方开始读取,从1开始标号 , 包含这一行
|
|
1387
|
-
start_line : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 包含这一行
|
|
1388
|
-
start_line_exclude : 从这个地方开始读取,从第一行开始找到这个字符串开始标记 , 不包含这一行, 返回的是一个 list(' '.join(one_line_list))
|
|
1389
|
-
end_index : 读取到这个地方结束,从1开始标号 , 不包含这一行
|
|
1390
|
-
end_line : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行
|
|
1391
|
-
end_line_exclude : 读取到这个地方结束,从第一行开始找到这个字符串开始标记 , 不包含这一行, 返回的是一个 list(' '.join(one_line_list))
|
|
1392
|
-
count : 读取指定的行数
|
|
1393
|
-
"""
|
|
1394
|
-
if file_is_empty(file_name=file_name):
|
|
1395
|
-
return []
|
|
1396
|
-
data_list = []
|
|
1397
|
-
# 普通文件的解析
|
|
1398
|
-
d_list = open(file_name, 'r', encoding='utf-8').readlines()
|
|
1399
|
-
# 数量
|
|
1400
|
-
c = 0
|
|
1401
|
-
start_flag = None
|
|
1402
|
-
end_flag = None
|
|
1403
|
-
if start_line is not None:
|
|
1404
|
-
start_flag = False
|
|
1405
|
-
if end_line is not None:
|
|
1406
|
-
end_flag = False
|
|
1407
|
-
for i in range(len(d_list)):
|
|
1408
|
-
line = d_list[i].strip()
|
|
1409
|
-
# 判断开始位置
|
|
1410
|
-
if start_index is not None and i + 1 < to_int(start_index):
|
|
1411
|
-
continue
|
|
1412
|
-
# 判断结束位置
|
|
1413
|
-
if end_index is not None and i + 1 >= to_int(end_index):
|
|
1414
|
-
continue
|
|
1415
|
-
# 判断数量
|
|
1416
|
-
if count is not None and c >= to_int(count):
|
|
1417
|
-
continue
|
|
1418
|
-
# 开始标记位
|
|
1419
|
-
if start_flag is not None and not start_flag and line.find(start_line) > -1:
|
|
1420
|
-
# 如果有标记位置,就是 True
|
|
1421
|
-
start_flag = True
|
|
1422
|
-
# 开始标记位
|
|
1423
|
-
if end_flag is not None and not end_flag and line.find(end_line) > -1:
|
|
1424
|
-
# 如果有标记位置,就是 True
|
|
1425
|
-
end_flag = True
|
|
1426
|
-
if start_flag is not None and not start_flag:
|
|
1427
|
-
# 有开始标记位参数,并且,还没有走到开始标记位
|
|
1428
|
-
continue
|
|
1429
|
-
elif end_flag is not None and end_flag:
|
|
1430
|
-
# 有结束标记位参数,并且,已经走到了结束标记位
|
|
1431
|
-
continue
|
|
1432
|
-
c += 1
|
|
1433
|
-
can_add = True
|
|
1434
|
-
if ignore_start_with is not None:
|
|
1435
|
-
if isinstance(ignore_start_with, list) or isinstance(ignore_start_with, set):
|
|
1436
|
-
for ss in ignore_start_with:
|
|
1437
|
-
if line.startswith(str(ss)):
|
|
1438
|
-
can_add = False
|
|
1439
|
-
elif isinstance(ignore_start_with, str):
|
|
1440
|
-
if line.startswith(str(ignore_start_with)):
|
|
1441
|
-
can_add = False
|
|
1442
|
-
if ignore_end_with is not None:
|
|
1443
|
-
if isinstance(ignore_end_with, list) or isinstance(ignore_end_with, set):
|
|
1444
|
-
for ss in ignore_end_with:
|
|
1445
|
-
if line.endswith(str(ss)):
|
|
1446
|
-
can_add = False
|
|
1447
|
-
elif isinstance(ignore_end_with, str):
|
|
1448
|
-
if line.endswith(str(ignore_end_with)):
|
|
1449
|
-
can_add = False
|
|
1450
|
-
if ignore_empty is not None and ignore_empty:
|
|
1451
|
-
if len(line) == 0:
|
|
1452
|
-
can_add = False
|
|
1453
|
-
if can_add:
|
|
1454
|
-
data_list.append(line)
|
|
1455
|
-
|
|
1456
|
-
# 更复杂的切分, 中间的部分 会转成 ' '.join(one_line_list)
|
|
1457
|
-
if start_line_exclude is not None and end_line_exclude is not None:
|
|
1458
|
-
data_list1 = data_list
|
|
1459
|
-
start_flag = False
|
|
1460
|
-
start_flag_once = False
|
|
1461
|
-
end_flag = False
|
|
1462
|
-
one_data_list = []
|
|
1463
|
-
data_list = []
|
|
1464
|
-
for i in range(len(data_list1)):
|
|
1465
|
-
line = data_list1[i].strip()
|
|
1466
|
-
# 开始标记位
|
|
1467
|
-
if not start_flag:
|
|
1468
|
-
if isinstance(start_line_exclude, list) or isinstance(start_line_exclude, set):
|
|
1469
|
-
for ss in start_line_exclude:
|
|
1470
|
-
if line.find(str(ss)) > -1:
|
|
1471
|
-
# 如果有标记位置,就是 True
|
|
1472
|
-
start_flag = True
|
|
1473
|
-
start_flag_once = True
|
|
1474
|
-
else:
|
|
1475
|
-
if line.find(str(start_line_exclude)) > -1:
|
|
1476
|
-
# 如果有标记位置,就是 True
|
|
1477
|
-
start_flag = True
|
|
1478
|
-
start_flag_once = True
|
|
1479
|
-
# 结束标记位
|
|
1480
|
-
if not end_flag:
|
|
1481
|
-
if isinstance(end_line_exclude, list) or isinstance(end_line_exclude, set):
|
|
1482
|
-
for ss in end_line_exclude:
|
|
1483
|
-
if line.find(str(ss)) > -1:
|
|
1484
|
-
# 如果有标记位置,就是 True
|
|
1485
|
-
end_flag = True
|
|
1486
|
-
else:
|
|
1487
|
-
if line.find(str(end_line_exclude)) > -1:
|
|
1488
|
-
# 如果有标记位置,就是 True
|
|
1489
|
-
end_flag = True
|
|
1490
|
-
if not start_flag:
|
|
1491
|
-
# 有开始标记位参数,并且,还没有走到开始标记位
|
|
1492
|
-
continue
|
|
1493
|
-
elif end_flag:
|
|
1494
|
-
# 有结束标记位参数,并且,已经走到了结束标记位
|
|
1495
|
-
start_flag = False
|
|
1496
|
-
end_flag = False
|
|
1497
|
-
start_flag_once = False
|
|
1498
|
-
# print(one_data_list)
|
|
1499
|
-
data_list.append(' '.join(one_data_list).strip())
|
|
1500
|
-
one_data_list = []
|
|
1501
|
-
continue
|
|
1502
|
-
# 去掉 start_line_exclude 包含行
|
|
1503
|
-
if start_flag_once:
|
|
1504
|
-
start_flag_once = False
|
|
1505
|
-
line = ''
|
|
1506
|
-
if start_flag and not end_flag:
|
|
1507
|
-
one_data_list.append(line)
|
|
1508
|
-
if len(one_data_list):
|
|
1509
|
-
data_list.append(' '.join(one_data_list).strip())
|
|
1510
|
-
|
|
1511
|
-
if sep_all is not None:
|
|
1512
|
-
# 全部划分, 重新分割成 list(str)
|
|
1513
|
-
data_list = ''.join(data_list).split(str(sep_all))
|
|
1514
|
-
# 有行分隔符, 将会把 list(str) 转化成 list(list)
|
|
1515
|
-
if len(list(filter(lambda x: x is not None,
|
|
1516
|
-
[sep_line, sep_line_prefix, sep_line_contain, sep_line_suffix, sep_line_with_space_count]))):
|
|
1517
|
-
# 当是这种情况的时候,返回的数据结果
|
|
1518
|
-
r_list = []
|
|
1519
|
-
# 数据中的一行 list 数据
|
|
1520
|
-
one_list = []
|
|
1521
|
-
# 空格数量
|
|
1522
|
-
space_count = 0
|
|
1523
|
-
for d_o in data_list:
|
|
1524
|
-
space_count = space_count + 1 if not d_o.strip() else 0
|
|
1525
|
-
# 过滤掉空行,无效行
|
|
1526
|
-
if len(d_o.strip()) and sep_is_front:
|
|
1527
|
-
one_list.append(d_o)
|
|
1528
|
-
# 这一行, 等于 sep_line
|
|
1529
|
-
if ((sep_line is not None and d_o == sep_line) or
|
|
1530
|
-
# 这一行, 包含 sep_line_contain
|
|
1531
|
-
(sep_line_contain is not None and d_o.find(sep_line_contain) != -1) or
|
|
1532
|
-
# 这一行, 是否是以 sep_line_prefix 开头
|
|
1533
|
-
(sep_line_prefix is not None and d_o.startswith(sep_line_prefix)) or
|
|
1534
|
-
# 这一行, 是否是以 sep_line_suffix 结尾
|
|
1535
|
-
(sep_line_suffix is not None and d_o.endswith(sep_line_suffix))):
|
|
1536
|
-
if len(one_list):
|
|
1537
|
-
r_list.append(one_list)
|
|
1538
|
-
one_list = []
|
|
1539
|
-
if len(d_o.strip()) and not sep_is_front:
|
|
1540
|
-
one_list.append(d_o)
|
|
1541
|
-
# 按照空格行的数量来进行分割
|
|
1542
|
-
if sep_line_with_space_count is not None and space_count == sep_line_with_space_count:
|
|
1543
|
-
if len(one_list):
|
|
1544
|
-
r_list.append(one_list)
|
|
1545
|
-
one_list = []
|
|
1546
|
-
space_count = 0
|
|
1547
|
-
# 最后的一条数据,兼容一下
|
|
1548
|
-
if len(one_list):
|
|
1549
|
-
r_list.append(one_list)
|
|
1550
|
-
data_list = r_list
|
|
1551
|
-
# 对这个 list 进行行内再次分割
|
|
1552
|
-
if sep is not None:
|
|
1553
|
-
r_list = []
|
|
1554
|
-
for line in data_list:
|
|
1555
|
-
# list(str) 情况
|
|
1556
|
-
if isinstance(line, str):
|
|
1557
|
-
r_list.append(line.split(str(sep)))
|
|
1558
|
-
# list(list) 情况
|
|
1559
|
-
elif isinstance(line, list):
|
|
1560
|
-
a_list = []
|
|
1561
|
-
for o_line in line:
|
|
1562
|
-
a_list.append(o_line.split(str(sep)))
|
|
1563
|
-
r_list.append(a_list)
|
|
1564
|
-
data_list = r_list
|
|
1565
|
-
# data_list 中的每一个元素都转化成 str
|
|
1566
|
-
if line_join is not None:
|
|
1567
|
-
data_list = list(map(lambda x: str(line_join).join(x), data_list))
|
|
1568
|
-
# data_list 中的每一个元素都转化成 先转化成str, 然后再转化成json
|
|
1569
|
-
if line_json is not None and line_json:
|
|
1570
|
-
data_list = list(map(lambda x:
|
|
1571
|
-
json.loads(str('' if line_join is None else line_join).join(x)),
|
|
1572
|
-
list(filter(lambda x: x is not None and len(str(x)), data_list))
|
|
1573
|
-
)
|
|
1574
|
-
)
|
|
1575
|
-
return data_list
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
# 读取文件中的数据,返回一个 str
|
|
1579
|
-
def to_str_from_file(file_name: str = 'a.txt',
|
|
1580
|
-
str_join: str = ' ',
|
|
1581
|
-
ignore_start_with: list | int | str | None = None,
|
|
1582
|
-
ignore_end_with: list | int | str | None = None,
|
|
1583
|
-
start_index: int = None,
|
|
1584
|
-
start_line: str = None,
|
|
1585
|
-
end_index: int = None,
|
|
1586
|
-
end_line: str = None,
|
|
1587
|
-
count: int = None) -> str:
|
|
1588
|
-
return to_data_from_file(file_name=file_name,
|
|
1589
|
-
ignore_start_with=ignore_start_with,
|
|
1590
|
-
ignore_end_with=ignore_end_with,
|
|
1591
|
-
str_join=str_join,
|
|
1592
|
-
start_index=start_index,
|
|
1593
|
-
start_line=start_line,
|
|
1594
|
-
end_index=end_index,
|
|
1595
|
-
end_line=end_line,
|
|
1596
|
-
count=count,
|
|
1597
|
-
r_str=True)
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
# 读取文件中的数据,返回一个 json
|
|
1601
|
-
def to_json_from_file(file_name: str = 'a.txt',
|
|
1602
|
-
start_index: int = None,
|
|
1603
|
-
start_line: str = None,
|
|
1604
|
-
end_index: int = None,
|
|
1605
|
-
end_line: str = None,
|
|
1606
|
-
count: int = None) -> dict[str, Any]:
|
|
1607
|
-
return to_data_from_file(file_name=file_name,
|
|
1608
|
-
start_index=start_index,
|
|
1609
|
-
start_line=start_line,
|
|
1610
|
-
end_index=end_index,
|
|
1611
|
-
end_line=end_line,
|
|
1612
|
-
ignore_start_with=['//', '/*', '#'],
|
|
1613
|
-
count=count,
|
|
1614
|
-
r_json=True)
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
def to_data_from_file(file_name: str = 'a.txt',
|
|
1618
|
-
sep: str = None,
|
|
1619
|
-
sep_line: str = None,
|
|
1620
|
-
sep_all: str = None,
|
|
1621
|
-
ignore_start_with: list | int | str | None = None,
|
|
1622
|
-
ignore_end_with: list | int | str | None = None,
|
|
1623
|
-
start_index: int = None,
|
|
1624
|
-
start_line: str = None,
|
|
1625
|
-
end_index: int = None,
|
|
1626
|
-
end_line: str = None,
|
|
1627
|
-
count: int = None,
|
|
1628
|
-
sheet_index: int = 1,
|
|
1629
|
-
column_index: list | int | str | None = None,
|
|
1630
|
-
column_date: list | int | str | None = None,
|
|
1631
|
-
column_datetime: list | int | str | None = None,
|
|
1632
|
-
r_json: bool = False,
|
|
1633
|
-
str_join: str = '',
|
|
1634
|
-
r_str: bool = False) -> str | dict[str, Any]:
|
|
1635
|
-
"""
|
|
1636
|
-
在 to_list 方法上再嵌套一层,
|
|
1637
|
-
r_str : 返回的数据是否是一个 字符串, ''.join(list)
|
|
1638
|
-
str_join : 返回的数据是否是一个 字符串, str_join.join(list), 用 str_join 做连接
|
|
1639
|
-
r_json : 返回的数据是否是一个 json 类型的数据
|
|
1640
|
-
"""
|
|
1641
|
-
d = to_list(file_name=file_name,
|
|
1642
|
-
sep=sep,
|
|
1643
|
-
sep_line=sep_line,
|
|
1644
|
-
sep_all=sep_all,
|
|
1645
|
-
ignore_start_with=ignore_start_with,
|
|
1646
|
-
ignore_end_with=ignore_end_with,
|
|
1647
|
-
start_index=start_index,
|
|
1648
|
-
start_line=start_line,
|
|
1649
|
-
end_index=end_index,
|
|
1650
|
-
end_line=end_line,
|
|
1651
|
-
count=count,
|
|
1652
|
-
sheet_index=sheet_index,
|
|
1653
|
-
column_index=column_index,
|
|
1654
|
-
column_date=column_date,
|
|
1655
|
-
column_datetime=column_datetime)
|
|
1656
|
-
return str_join.join(d) if r_str else json.loads(str_join.join(d)) if r_json else d
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
# 将文件导出成excel格式的
|
|
1660
|
-
def to_excel(data_list: set | list | tuple | None,
|
|
1661
|
-
file_name: str = None,
|
|
1662
|
-
file_path: str = 'excel') -> None:
|
|
1663
|
-
if file_name is None:
|
|
1664
|
-
file_name = 'excel'
|
|
1665
|
-
file_name = str(file_name)
|
|
1666
|
-
while file_path.endswith('/'):
|
|
1667
|
-
file_path = file_path[0:-1]
|
|
1668
|
-
check_file(file_path)
|
|
1669
|
-
# 实例化对象excel对象
|
|
1670
|
-
excel_obj = openpyxl.Workbook()
|
|
1671
|
-
# excel 内第一个sheet工作表
|
|
1672
|
-
excel_obj_sheet = excel_obj[excel_obj.sheetnames[0]]
|
|
1673
|
-
# 给单元格赋值
|
|
1674
|
-
for one_data in data_list:
|
|
1675
|
-
s_list = []
|
|
1676
|
-
if isinstance(one_data, list) or isinstance(one_data, set):
|
|
1677
|
-
for one in one_data:
|
|
1678
|
-
if isinstance(one, dict) or isinstance(one, list):
|
|
1679
|
-
s = json.dumps(one)
|
|
1680
|
-
else:
|
|
1681
|
-
s = str(one)
|
|
1682
|
-
s_list.append(s)
|
|
1683
|
-
excel_obj_sheet.append(s_list)
|
|
1684
|
-
else:
|
|
1685
|
-
if is_json_serializable(one_data):
|
|
1686
|
-
s = json.dumps(one_data)
|
|
1687
|
-
else:
|
|
1688
|
-
s = str(one_data)
|
|
1689
|
-
excel_obj_sheet.append([s])
|
|
1690
|
-
|
|
1691
|
-
# 文件保存
|
|
1692
|
-
excel_obj.save(file_path + '/' + get_file_name(file_name, '.xlsx', True))
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
def to_csv(data_list: set | list | tuple | dict,
|
|
1696
|
-
file_name: str = None,
|
|
1697
|
-
file_path: str = 'csv') -> None:
|
|
1698
|
-
"""
|
|
1699
|
-
将文件导出成csv格式的
|
|
1700
|
-
data_list 格式
|
|
1701
|
-
data_list = [['Name', 'Age', 'Gender'],
|
|
1702
|
-
['Alice', 25, 'Female'],
|
|
1703
|
-
['Bob', 30, 'Male'],
|
|
1704
|
-
['Charlie', 35, 'Male']]
|
|
1705
|
-
data_list = [{
|
|
1706
|
-
"a": 1,
|
|
1707
|
-
"b": 2,
|
|
1708
|
-
},{
|
|
1709
|
-
"a": 1,
|
|
1710
|
-
"b": 2,
|
|
1711
|
-
}]
|
|
1712
|
-
file_name = 'data'
|
|
1713
|
-
"""
|
|
1714
|
-
if file_name is None:
|
|
1715
|
-
file_name = 'csv'
|
|
1716
|
-
file_name = get_file_name(file_name, '.csv', True)
|
|
1717
|
-
while file_path.endswith('/'):
|
|
1718
|
-
file_path = file_path[0:-1]
|
|
1719
|
-
check_file(file_path)
|
|
1720
|
-
d_list = []
|
|
1721
|
-
if isinstance(data_list, tuple):
|
|
1722
|
-
d_list = list(data_list)
|
|
1723
|
-
else:
|
|
1724
|
-
if len(data_list) and (isinstance(data_list[0], dict) or isinstance(data_list[0], tuple)):
|
|
1725
|
-
title_list = []
|
|
1726
|
-
for key in data_list[0]:
|
|
1727
|
-
title_list.append(key)
|
|
1728
|
-
d_list.append(title_list)
|
|
1729
|
-
for one_data in data_list:
|
|
1730
|
-
one_list = []
|
|
1731
|
-
for k in title_list:
|
|
1732
|
-
one_list.append(one_data[k])
|
|
1733
|
-
d_list.append(one_list)
|
|
1734
|
-
else:
|
|
1735
|
-
d_list = data_list
|
|
1736
|
-
with open(file_path + '/' + file_name, 'w', newline='') as f:
|
|
1737
|
-
writer = csv.writer(f)
|
|
1738
|
-
writer.writerows(d_list)
|