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