upplib 3.3.7__tar.gz → 3.3.8__tar.gz
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-3.3.7 → upplib-3.3.8}/PKG-INFO +1 -1
- {upplib-3.3.7 → upplib-3.3.8}/upplib/__init__.py +1 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/file.py +48 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/file_to_text.py +4 -434
- upplib-3.3.8/upplib/text_to_file.py +389 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib.egg-info/PKG-INFO +1 -1
- upplib-3.3.7/upplib/text_to_file.py +0 -3
- {upplib-3.3.7 → upplib-3.3.8}/LICENSE +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/README.md +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/pyproject.toml +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/setup.cfg +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/setup.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/chart.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/chart_html.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/clean_up_msg.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/common_package.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/config_data.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/db.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/format_data.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/http_util.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/index.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/mail.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/mail_html.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/markdown.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/multi_thread.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/query_log.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/redis_tool.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib/util.py +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib.egg-info/SOURCES.txt +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib.egg-info/dependency_links.txt +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib.egg-info/requires.txt +0 -0
- {upplib-3.3.7 → upplib-3.3.8}/upplib.egg-info/top_level.txt +0 -0
|
@@ -38,6 +38,38 @@ def get_file(file_path: str = None,
|
|
|
38
38
|
return r_list
|
|
39
39
|
|
|
40
40
|
|
|
41
|
+
def get_latest_file(file_path: str = None,
|
|
42
|
+
path_startswith: str = None,
|
|
43
|
+
startswith: str = None,
|
|
44
|
+
path_contain: str = None,
|
|
45
|
+
contain: str = None,
|
|
46
|
+
path_endswith: str = None,
|
|
47
|
+
endswith: str = None,
|
|
48
|
+
full_path: bool = None) -> None | tuple[str, str] | str:
|
|
49
|
+
"""
|
|
50
|
+
按照文件名字排序,获得最新的一个文件
|
|
51
|
+
full_path: 是否返回完整的路径
|
|
52
|
+
"""
|
|
53
|
+
html_list = get_file(file_path=file_path,
|
|
54
|
+
path_startswith=path_startswith,
|
|
55
|
+
startswith=startswith,
|
|
56
|
+
path_contain=path_contain,
|
|
57
|
+
contain=contain,
|
|
58
|
+
sort_asc=False,
|
|
59
|
+
path_endswith=path_endswith,
|
|
60
|
+
endswith=endswith)
|
|
61
|
+
r1 = html_list[-1] if len(html_list) > 0 else None
|
|
62
|
+
if r1 is None:
|
|
63
|
+
return None
|
|
64
|
+
r1_short = r1
|
|
65
|
+
file_sep = '\\' if is_win() else '/'
|
|
66
|
+
if file_sep in r1:
|
|
67
|
+
r1_short = r1.split(file_sep)[-1]
|
|
68
|
+
if full_path is None:
|
|
69
|
+
return r1_short, r1
|
|
70
|
+
return r1 if full_path else r1_short
|
|
71
|
+
|
|
72
|
+
|
|
41
73
|
def get_file_folder(file_name_one: str = None) -> str:
|
|
42
74
|
"""
|
|
43
75
|
返回这个文件的文件夹路径
|
|
@@ -213,6 +245,22 @@ def find_file_by_content(file_path: str = '',
|
|
|
213
245
|
to_log(one_file, e)
|
|
214
246
|
continue
|
|
215
247
|
|
|
248
|
+
|
|
249
|
+
def check_file(file_name: str = None) -> None:
|
|
250
|
+
r"""
|
|
251
|
+
检查文件夹是否存在,不存在,就创建新的
|
|
252
|
+
支持多级目录 , 例如: C:\Users\yangpu\Desktop\study\a\b\c\d\e\f
|
|
253
|
+
"""
|
|
254
|
+
if file_name is None or file_name == '':
|
|
255
|
+
return
|
|
256
|
+
file_sep = '\\' if is_win() else '/'
|
|
257
|
+
f_n = file_name.split(file_sep)
|
|
258
|
+
for i in range(1, len(f_n) + 1):
|
|
259
|
+
# C:\Users\yangpu\Desktop\study\p.t
|
|
260
|
+
p_n = file_sep.join(f_n[0:i])
|
|
261
|
+
if p_n and not os.path.exists(p_n):
|
|
262
|
+
os.mkdir(p_n)
|
|
263
|
+
|
|
216
264
|
# print(get_file_data_line(r'D:\notepad_file\202306\a.txt', 'payout', from_last=False))
|
|
217
265
|
|
|
218
266
|
# file_all = get_file(r'C:\Users\yang\Desktop\ticket\no.use', path_contain='03')
|
|
@@ -3,358 +3,6 @@ from upplib.common_package import *
|
|
|
3
3
|
from upplib.file import get_file
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
def get_latest_file(file_path: str = None,
|
|
7
|
-
path_startswith: str = None,
|
|
8
|
-
startswith: str = None,
|
|
9
|
-
path_contain: str = None,
|
|
10
|
-
contain: str = None,
|
|
11
|
-
path_endswith: str = None,
|
|
12
|
-
endswith: 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_startswith=path_startswith,
|
|
20
|
-
startswith=startswith,
|
|
21
|
-
path_contain=path_contain,
|
|
22
|
-
contain=contain,
|
|
23
|
-
sort_asc=False,
|
|
24
|
-
path_endswith=path_endswith,
|
|
25
|
-
endswith=endswith)
|
|
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
6
|
def to_list(file_name: str = 'a.txt',
|
|
359
7
|
sep: str = None,
|
|
360
8
|
sep_line: str = None,
|
|
@@ -364,6 +12,7 @@ def to_list(file_name: str = 'a.txt',
|
|
|
364
12
|
sep_all: str = None,
|
|
365
13
|
line_ignore_start_with: list[str] | set[str] | str = None,
|
|
366
14
|
line_ignore_end_with: list[str] | set[str] | str | None = None,
|
|
15
|
+
line_ignore_empty: bool | None = None,
|
|
367
16
|
start_index: int = None,
|
|
368
17
|
start_line: str = None,
|
|
369
18
|
end_index: int = None,
|
|
@@ -409,6 +58,7 @@ def to_list(file_name: str = 'a.txt',
|
|
|
409
58
|
sep_all=sep_all,
|
|
410
59
|
line_ignore_start_with=line_ignore_start_with,
|
|
411
60
|
line_ignore_end_with=line_ignore_end_with,
|
|
61
|
+
line_ignore_empty=line_ignore_empty,
|
|
412
62
|
start_index=start_index,
|
|
413
63
|
start_line=start_line,
|
|
414
64
|
end_index=end_index,
|
|
@@ -859,6 +509,7 @@ def to_data_from_file(file_name: str = 'a.txt',
|
|
|
859
509
|
sep_all: str = None,
|
|
860
510
|
line_ignore_start_with: list | int | str | None = None,
|
|
861
511
|
line_ignore_end_with: list | int | str | None = None,
|
|
512
|
+
line_ignore_empty: bool | None = None,
|
|
862
513
|
start_index: int = None,
|
|
863
514
|
start_line: str = None,
|
|
864
515
|
end_index: int = None,
|
|
@@ -883,6 +534,7 @@ def to_data_from_file(file_name: str = 'a.txt',
|
|
|
883
534
|
sep_all=sep_all,
|
|
884
535
|
line_ignore_start_with=line_ignore_start_with,
|
|
885
536
|
line_ignore_end_with=line_ignore_end_with,
|
|
537
|
+
line_ignore_empty=line_ignore_empty,
|
|
886
538
|
start_index=start_index,
|
|
887
539
|
start_line=start_line,
|
|
888
540
|
end_index=end_index,
|
|
@@ -893,85 +545,3 @@ def to_data_from_file(file_name: str = 'a.txt',
|
|
|
893
545
|
column_date=column_date,
|
|
894
546
|
column_datetime=column_datetime)
|
|
895
547
|
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)
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
from upplib import *
|
|
2
|
+
from upplib.common_package import *
|
|
3
|
+
from upplib.file import get_file
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def to_print(*args,
|
|
7
|
+
time_prefix: bool = False,
|
|
8
|
+
line_with_space_count: int = None,
|
|
9
|
+
interval: int = None) -> str:
|
|
10
|
+
"""
|
|
11
|
+
记录日志, 如果是对象会转化为 json
|
|
12
|
+
数据直接 print, 不记录到文件
|
|
13
|
+
例如: aaa
|
|
14
|
+
interval: 间隔一段时间,打印一下, 单位: 秒,不要频繁打印
|
|
15
|
+
time_prefix : 是否在前面加时间, 默认 False
|
|
16
|
+
"""
|
|
17
|
+
d = ' '.join(map(lambda x: json.dumps(x) if is_json_serializable(x) else str(x), args))
|
|
18
|
+
d = d.strip()
|
|
19
|
+
lo = datetime.today().strftime('%Y-%m-%d %H:%M:%S') + ' ' + d if time_prefix is True else d
|
|
20
|
+
if lo is None or str(lo) == '':
|
|
21
|
+
lo = to_datetime(r_str=True)
|
|
22
|
+
prefix_space = ' ' * (line_with_space_count or 0)
|
|
23
|
+
if interval is None or get_timestamp() - get_thread_local_index_data().get('to_print_time', 0) >= interval:
|
|
24
|
+
get_thread_local_index_data()['to_print_time'] = get_timestamp()
|
|
25
|
+
s = ''
|
|
26
|
+
if interval is not None:
|
|
27
|
+
s = str(to_datetime()) + ' '
|
|
28
|
+
print(prefix_space + s + str(lo))
|
|
29
|
+
return prefix_space + lo
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def to_log(*args,
|
|
33
|
+
time_prefix: bool = False,
|
|
34
|
+
line_with_space_count: int = None,
|
|
35
|
+
interval: int = None) -> str:
|
|
36
|
+
"""
|
|
37
|
+
记录日志, 如果是对象会转化为 json
|
|
38
|
+
前面加了时间
|
|
39
|
+
例如: 2024-11-07 10:23:47 aaa
|
|
40
|
+
"""
|
|
41
|
+
return to_print(*args,
|
|
42
|
+
time_prefix=time_prefix if time_prefix is not None else True,
|
|
43
|
+
line_with_space_count=line_with_space_count,
|
|
44
|
+
interval=interval)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def to_print_file(*args,
|
|
48
|
+
file_path: str = None,
|
|
49
|
+
file_name: str = None,
|
|
50
|
+
file_name_with_date: bool = False,
|
|
51
|
+
file_name_prefix: str = None,
|
|
52
|
+
file_name_suffix: str = None,
|
|
53
|
+
line_with_space_count: int = None,
|
|
54
|
+
mode: str = 'a',
|
|
55
|
+
interval: int = None) -> str:
|
|
56
|
+
"""
|
|
57
|
+
将 print 数据, 写入到 print_file 文件
|
|
58
|
+
文件按照 日期自动创建
|
|
59
|
+
例如: print_file/2020-01-01.txt
|
|
60
|
+
to_print_file(query_string, mode='w', file_path=file_path, file_name=get_file_name(file_name=file_path, is_date=True))
|
|
61
|
+
"""
|
|
62
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
63
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
64
|
+
file_path=file_path,
|
|
65
|
+
file_name=file_name,
|
|
66
|
+
file_name_prefix=file_name_prefix,
|
|
67
|
+
file_name_suffix=file_name_suffix,
|
|
68
|
+
interval=interval,
|
|
69
|
+
mode=mode,
|
|
70
|
+
file_name_with_date=file_name_with_date,
|
|
71
|
+
line_with_space_count=line_with_space_count,
|
|
72
|
+
fun_name=to_print_file
|
|
73
|
+
)
|
|
74
|
+
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
75
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
76
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
77
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
78
|
+
file_path=str(file_path if file_path is not None else 'to_print_file'),
|
|
79
|
+
mode=mode,
|
|
80
|
+
fixed_name=True,
|
|
81
|
+
suffix='.txt')
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def to_print_txt(*args,
|
|
85
|
+
file_path: str = None,
|
|
86
|
+
file_name_with_date: bool = False,
|
|
87
|
+
file_name: str = None,
|
|
88
|
+
file_name_prefix: str = None,
|
|
89
|
+
file_name_suffix: str = None,
|
|
90
|
+
line_with_space_count: int = None,
|
|
91
|
+
mode: str = 'a',
|
|
92
|
+
interval: int = None) -> str:
|
|
93
|
+
"""
|
|
94
|
+
将 print 数据, 写入到 print_txt 文件
|
|
95
|
+
文件按照 日期自动创建
|
|
96
|
+
例如: print_txt/2020-01-01.txt
|
|
97
|
+
"""
|
|
98
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
99
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
100
|
+
file_path=file_path,
|
|
101
|
+
file_name=file_name,
|
|
102
|
+
file_name_prefix=file_name_prefix,
|
|
103
|
+
file_name_suffix=file_name_suffix,
|
|
104
|
+
interval=interval,
|
|
105
|
+
mode=mode,
|
|
106
|
+
file_name_with_date=file_name_with_date,
|
|
107
|
+
line_with_space_count=line_with_space_count,
|
|
108
|
+
fun_name=to_print_txt
|
|
109
|
+
)
|
|
110
|
+
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
111
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
112
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
113
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
114
|
+
file_path=str(file_path if file_path is not None else 'to_print_txt'),
|
|
115
|
+
mode=mode,
|
|
116
|
+
fixed_name=True,
|
|
117
|
+
suffix='.txt')
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def to_log_file(*args,
|
|
121
|
+
file_path: str = None,
|
|
122
|
+
file_name_with_date: bool = False,
|
|
123
|
+
file_name: str = None,
|
|
124
|
+
file_name_prefix: str = None,
|
|
125
|
+
file_name_suffix: str = None,
|
|
126
|
+
line_with_space_count: int = None,
|
|
127
|
+
time_prefix: bool = True,
|
|
128
|
+
mode: str = 'a',
|
|
129
|
+
interval: int = None) -> None:
|
|
130
|
+
"""
|
|
131
|
+
将 log 数据, 写入到 log_file 文件
|
|
132
|
+
文件按照 日期自动创建
|
|
133
|
+
例如: log_file/2020-01-01.log
|
|
134
|
+
"""
|
|
135
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
136
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
137
|
+
file_path=file_path,
|
|
138
|
+
file_name=file_name,
|
|
139
|
+
file_name_prefix=file_name_prefix,
|
|
140
|
+
file_name_suffix=file_name_suffix,
|
|
141
|
+
interval=interval,
|
|
142
|
+
mode=mode,
|
|
143
|
+
file_name_with_date=file_name_with_date,
|
|
144
|
+
line_with_space_count=line_with_space_count,
|
|
145
|
+
fun_name=to_log_file
|
|
146
|
+
)
|
|
147
|
+
to_txt(data_param=[
|
|
148
|
+
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
149
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
150
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
151
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
152
|
+
file_path=str(file_path if file_path is not None else 'to_log_file'),
|
|
153
|
+
fixed_name=True,
|
|
154
|
+
mode=mode,
|
|
155
|
+
suffix='.log')
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def to_log_txt(*args,
|
|
159
|
+
file_path: str = None,
|
|
160
|
+
file_name_with_date: bool = False,
|
|
161
|
+
file_name: str = None,
|
|
162
|
+
file_name_prefix: str = None,
|
|
163
|
+
file_name_suffix: str = None,
|
|
164
|
+
line_with_space_count: int = None,
|
|
165
|
+
time_prefix: bool = True,
|
|
166
|
+
mode: str = 'a',
|
|
167
|
+
interval: int = None) -> None:
|
|
168
|
+
"""
|
|
169
|
+
将 log 数据, 写入到 log_txt 文件夹中
|
|
170
|
+
文件按照 日期自动创建
|
|
171
|
+
例如: log_txt/2020-01-01.txt
|
|
172
|
+
"""
|
|
173
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
174
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
175
|
+
file_path=file_path,
|
|
176
|
+
file_name=file_name,
|
|
177
|
+
file_name_prefix=file_name_prefix,
|
|
178
|
+
file_name_suffix=file_name_suffix,
|
|
179
|
+
interval=interval,
|
|
180
|
+
mode=mode,
|
|
181
|
+
file_name_with_date=file_name_with_date,
|
|
182
|
+
line_with_space_count=line_with_space_count,
|
|
183
|
+
fun_name=to_log_txt
|
|
184
|
+
)
|
|
185
|
+
to_txt(data_param=[
|
|
186
|
+
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
187
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
188
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
189
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
190
|
+
file_path=str(file_path if file_path is not None else 'to_log_txt'),
|
|
191
|
+
mode=mode,
|
|
192
|
+
fixed_name=True,
|
|
193
|
+
suffix='.txt')
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def to_txt(data_param: Any,
|
|
197
|
+
file_name: str = 'txt',
|
|
198
|
+
file_path: str = 'txt',
|
|
199
|
+
fixed_name: bool = False,
|
|
200
|
+
mode: str = 'a',
|
|
201
|
+
suffix: str = '.txt',
|
|
202
|
+
sep_list: str = '\t',
|
|
203
|
+
file_name_is_date: bool = False) -> str:
|
|
204
|
+
r"""
|
|
205
|
+
将 list 中的数据以 json 或者基本类型的形式写入到文件中
|
|
206
|
+
data_param : 数组数据, 也可以不是数组
|
|
207
|
+
file_name : 文件名 , 默认 txt
|
|
208
|
+
当文件名是 C:\Users\yangpu\Desktop\study\abc\d\e\f\a.sql 这种类型的时候, 可以直接创建文件夹,
|
|
209
|
+
会赋值 file_name=a,
|
|
210
|
+
file_path=C:\Users\yangpu\Desktop\study\abc\d\e\f,
|
|
211
|
+
fixed_name=True,
|
|
212
|
+
suffix=.sql
|
|
213
|
+
当文件名是 abc 的时候, 按照正常值,计算
|
|
214
|
+
file_path : 文件路径
|
|
215
|
+
fixed_name : 是否固定文件名
|
|
216
|
+
suffix : 文件后缀, 默认 .txt
|
|
217
|
+
sep_list : 当 data_param 是 list(list) 类型的时候 使用 sep_list 作为分割内部的分隔符,
|
|
218
|
+
默认使用 \t 作为分隔符, 如果为 None , 则按照 json 去处理这个 list
|
|
219
|
+
"""
|
|
220
|
+
file_name = str(file_name)
|
|
221
|
+
for sep in ['\\', '/']:
|
|
222
|
+
f_n = file_name.split(sep)
|
|
223
|
+
if len(f_n) > 1:
|
|
224
|
+
file_name = f_n[-1]
|
|
225
|
+
file_path = sep.join(f_n[0:-1])
|
|
226
|
+
if '.' in file_name:
|
|
227
|
+
suffix = '.' + file_name.split('.')[-1]
|
|
228
|
+
file_name = file_name[0:file_name.rfind('.')]
|
|
229
|
+
fixed_name = True
|
|
230
|
+
|
|
231
|
+
# 检查路径 file_path
|
|
232
|
+
while file_path.endswith('/'):
|
|
233
|
+
file_path = file_path[0:-1]
|
|
234
|
+
check_file(file_path)
|
|
235
|
+
|
|
236
|
+
# 在 file_name 中, 检查是否有后缀
|
|
237
|
+
if '.' in file_name:
|
|
238
|
+
suffix = '.' + file_name.split('.')[-1]
|
|
239
|
+
file_name = file_name[0:file_name.rfind('.')]
|
|
240
|
+
|
|
241
|
+
# 生成 file_name
|
|
242
|
+
if fixed_name:
|
|
243
|
+
file_name = file_name + suffix
|
|
244
|
+
else:
|
|
245
|
+
file_name = get_file_name(file_name, suffix, is_date=file_name_is_date)
|
|
246
|
+
# 文件路径
|
|
247
|
+
file_name_path = file_name
|
|
248
|
+
if file_path != '':
|
|
249
|
+
file_name_path = file_path + '/' + file_name
|
|
250
|
+
# 写入文件
|
|
251
|
+
text_file = open(file_name_path, mode, encoding='utf-8')
|
|
252
|
+
if isinstance(data_param, set):
|
|
253
|
+
data_param = list(data_param)
|
|
254
|
+
if not isinstance(data_param, list):
|
|
255
|
+
text_file.write(to_str(data_param) + '\n')
|
|
256
|
+
else:
|
|
257
|
+
for one in data_param:
|
|
258
|
+
if isinstance(one, (list, tuple, set)) and sep_list is not None:
|
|
259
|
+
text_file.write(str(sep_list).join(list(map(lambda x: to_str(x), one))) + '\n')
|
|
260
|
+
else:
|
|
261
|
+
text_file.write(to_str(one) + '\n')
|
|
262
|
+
text_file.close()
|
|
263
|
+
return file_name_path
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
267
|
+
def to_txt_file(data_param: Any,
|
|
268
|
+
file_name: str = None,
|
|
269
|
+
mode: str = 'a') -> str:
|
|
270
|
+
file_name = get_file_name('to_txt_file', is_date=True) if file_name is None else file_name
|
|
271
|
+
suffix = '.txt'
|
|
272
|
+
f = file_name
|
|
273
|
+
for sep in ['\\', '/']:
|
|
274
|
+
f_n = file_name.split(sep)
|
|
275
|
+
if len(f_n) > 1:
|
|
276
|
+
f = file_name
|
|
277
|
+
if '.' in f:
|
|
278
|
+
suffix = '.' + f.split('.')[-1]
|
|
279
|
+
file_name = file_name.replace(suffix, '')
|
|
280
|
+
return to_txt(data_param=data_param,
|
|
281
|
+
file_name=file_name,
|
|
282
|
+
file_path='to_txt_file',
|
|
283
|
+
suffix=suffix,
|
|
284
|
+
fixed_name=True,
|
|
285
|
+
mode=mode)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
289
|
+
def to_file(data_param: Any,
|
|
290
|
+
file_name: str = None,
|
|
291
|
+
mode: str = 'a') -> str:
|
|
292
|
+
file_name = get_file_name('to_file', is_date=True) if file_name is None else file_name
|
|
293
|
+
suffix = '.txt'
|
|
294
|
+
f = file_name
|
|
295
|
+
for sep in ['\\', '/']:
|
|
296
|
+
f_n = file_name.split(sep)
|
|
297
|
+
if len(f_n) > 1:
|
|
298
|
+
f = file_name
|
|
299
|
+
if '.' in f:
|
|
300
|
+
suffix = '.' + f.split('.')[-1]
|
|
301
|
+
file_name = file_name.replace(suffix, '')
|
|
302
|
+
return to_txt(data_param=data_param,
|
|
303
|
+
file_name=file_name,
|
|
304
|
+
file_path='file',
|
|
305
|
+
suffix=suffix,
|
|
306
|
+
fixed_name=True,
|
|
307
|
+
mode=mode)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
# 将文件导出成excel格式的
|
|
311
|
+
def to_excel(data_list: set | list | tuple | None,
|
|
312
|
+
file_name: str = None,
|
|
313
|
+
file_path: str = 'excel') -> None:
|
|
314
|
+
if file_name is None:
|
|
315
|
+
file_name = 'excel'
|
|
316
|
+
file_name = str(file_name)
|
|
317
|
+
while file_path.endswith('/'):
|
|
318
|
+
file_path = file_path[0:-1]
|
|
319
|
+
check_file(file_path)
|
|
320
|
+
# 实例化对象excel对象
|
|
321
|
+
excel_obj = openpyxl.Workbook()
|
|
322
|
+
# excel 内第一个sheet工作表
|
|
323
|
+
excel_obj_sheet = excel_obj[excel_obj.sheetnames[0]]
|
|
324
|
+
# 给单元格赋值
|
|
325
|
+
for one_data in data_list:
|
|
326
|
+
s_list = []
|
|
327
|
+
if isinstance(one_data, list) or isinstance(one_data, set):
|
|
328
|
+
for one in one_data:
|
|
329
|
+
if isinstance(one, dict) or isinstance(one, list):
|
|
330
|
+
s = json.dumps(one)
|
|
331
|
+
else:
|
|
332
|
+
s = str(one)
|
|
333
|
+
s_list.append(s)
|
|
334
|
+
excel_obj_sheet.append(s_list)
|
|
335
|
+
else:
|
|
336
|
+
if is_json_serializable(one_data):
|
|
337
|
+
s = json.dumps(one_data)
|
|
338
|
+
else:
|
|
339
|
+
s = str(one_data)
|
|
340
|
+
excel_obj_sheet.append([s])
|
|
341
|
+
|
|
342
|
+
# 文件保存
|
|
343
|
+
excel_obj.save(file_path + '/' + get_file_name(file_name, '.xlsx', True))
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def to_csv(data_list: set | list | tuple | dict,
|
|
347
|
+
file_name: str = None,
|
|
348
|
+
file_path: str = 'csv') -> None:
|
|
349
|
+
"""
|
|
350
|
+
将文件导出成csv格式的
|
|
351
|
+
data_list 格式
|
|
352
|
+
data_list = [['Name', 'Age', 'Gender'],
|
|
353
|
+
['Alice', 25, 'Female'],
|
|
354
|
+
['Bob', 30, 'Male'],
|
|
355
|
+
['Charlie', 35, 'Male']]
|
|
356
|
+
data_list = [{
|
|
357
|
+
"a": 1,
|
|
358
|
+
"b": 2,
|
|
359
|
+
},{
|
|
360
|
+
"a": 1,
|
|
361
|
+
"b": 2,
|
|
362
|
+
}]
|
|
363
|
+
file_name = 'data'
|
|
364
|
+
"""
|
|
365
|
+
if file_name is None:
|
|
366
|
+
file_name = 'csv'
|
|
367
|
+
file_name = get_file_name(file_name, '.csv', True)
|
|
368
|
+
while file_path.endswith('/'):
|
|
369
|
+
file_path = file_path[0:-1]
|
|
370
|
+
check_file(file_path)
|
|
371
|
+
d_list = []
|
|
372
|
+
if isinstance(data_list, tuple):
|
|
373
|
+
d_list = list(data_list)
|
|
374
|
+
else:
|
|
375
|
+
if len(data_list) and (isinstance(data_list[0], dict) or isinstance(data_list[0], tuple)):
|
|
376
|
+
title_list = []
|
|
377
|
+
for key in data_list[0]:
|
|
378
|
+
title_list.append(key)
|
|
379
|
+
d_list.append(title_list)
|
|
380
|
+
for one_data in data_list:
|
|
381
|
+
one_list = []
|
|
382
|
+
for k in title_list:
|
|
383
|
+
one_list.append(one_data[k])
|
|
384
|
+
d_list.append(one_list)
|
|
385
|
+
else:
|
|
386
|
+
d_list = data_list
|
|
387
|
+
with open(file_path + '/' + file_name, 'w', newline='') as f:
|
|
388
|
+
writer = csv.writer(f)
|
|
389
|
+
writer.writerows(d_list)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|