-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
200 lines (141 loc) · 6.17 KB
/
Code.py
File metadata and controls
200 lines (141 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
```
'''
读取DB2数据表格,并转译为Excel
@Author: J. X. PENG (Alreitetrs Pourton)
@Date: 2024.02.02
'''
import openpyxl
import re
if __name__ == '__main__':
# 使用 'r' 参数来读取文件
try:
# 提示用户输入文件名
file_name = input("请输入文件名: ")
# 尝试以读取模式打开文件,并指定编码为 Windows-1252
with open(file_name, 'r', encoding='Windows-1252') as file:
content = file.read()
# 正则表达式提取
# 开始位置
start_marker = 'CREATE TABLE'
# 结束位置
end_marker = 'COMMENT ON '
# 使用正则表达式找到起始和结束位置
start_pos = re.search(start_marker, content).end()
end_pos = re.search(end_marker, content[start_pos:]).start() + start_pos
# 提取特定范围内的内容
text = content[start_pos:end_pos]
# 提取****名
# 使用正则表达式匹配双引号内的内容
matches = re.findall(r'"([^"]*)"', text)
# 提取**类型
# 提取第一个左括号的位置
matchbracket = re.search(r'\(', content[start_pos:end_pos])
# 定义 start_pos2
start_pos2 = matchbracket.start() + start_pos
# 提取特定范围内的内容
text2 = content[start_pos2:end_pos]
# 使用逗号分割文本
split_text = text2.split(',')
# 替换每个字符串中的"/n/t/t"为空白
split_text2 = [item.replace("\n\t\t", " ") for item in split_text]
# 去除每个字符串两端的空格
split_text3 = [item.strip() for item in split_text2]
# 对每个字符串使用空格分割,并取第一个空格后的字符串
matches2 = [re.findall(r'\b[A-Za-z]+\b', item)[0] for item in split_text3]
# 提取**长度
# 初始化一个空列表来存储结果
matches3 = []
# 首先,按逗号分隔文本字符串
text_length = text2.split(',')
# 使用for循环遍历列表中的每一项
for item2 in text_length:
# 对每一项应用正则表达式
match3 = re.search(r'\((\d+)\)', item2)
if match3:
# 如果找到匹配项,将括号内的数字添加到结果列表中
matches3.append(match3.group(1))
else:
# 如果没有找到匹配项,添加一个空字符串到结果列表中
matches3.append('')
# 正则表达式提取另一个范围
start_marker2 = 'PRIMARY KEY' # 开始位置
end_marker2 = 'SET' # 结束位置
# 使用正则表达式找到起始和结束位置
start_pos2 = re.search(start_marker2, content).end()
end_pos2 = re.search(end_marker2, content[start_pos2:]).start() + start_pos2
# 提取特定范围内的内容
text_new = content[start_pos2:end_pos2]
# 提取**
# 使用正则表达式匹配双引号内的内容
matches4_ = re.findall(r'"([^"]*)"', text_new)
# 去除最后两项(最后两项为文件名)
matches4_ = matches4_[:-2]
# 开始遍历从matches的第三项(索引为2)开始
matches4 = ['true' if match in matches4_ else 'false' for match in matches[2:-2]]
# 提取是否**
# 判断每一项是否有“NOT NULL"
matches5 = ["true" if "NOT NULL" in item else "false" for item in split_text2]
# 提取****(均为“3”)
matches6 = [3 for _ in matches[2:-2]]
# 提取****(均为“0”)
matches7 = [0 for _ in matches[2:-2]]
# 提取**(均为“false”)
matches8 = ["false" for _ in matches[2:-2]]
# 写入Excel表格
# 打开Excel文件
workbook = openpyxl.load_workbook('/Users/alreiters/Desktop/hiveImportTemplate.xlsx')
worksheet = workbook.active
# 写入****名
# 写入前两行,因为这两项为文件名(注意:这里使用正确的行索引1和2)
for i in range(1, 3): # Excel的行索引从1开始
worksheet.cell(row=i, column=1, value=matches[i - 1]) # 减1以匹配matches的索引
# 从第三行开始写入到倒数第三行,因为最后两项为表名和索引名
for idx, match in enumerate(matches[2:-2], start=10): # 从第三项开始,并设置start=3以匹配Excel的行索引
worksheet.cell(row=idx, column=1, value=match)
# 写入**类型
for idx2, match2 in enumerate(matches2, start=10):
worksheet.cell(row=idx2, column=3, value=match2)
# 写入**类型
for idx3, match3 in enumerate(matches3, start=10):
worksheet.cell(row=idx3, column=4, value=match3)
# 写入**
for idx4, match4 in enumerate(matches4, start=10):
worksheet.cell(row=idx4, column=5, value=match4)
# 写入****
for idx5, match5 in enumerate(matches5, start=10):
worksheet.cell(row=idx5, column=7, value=match5)
# 写入****
for idx6, match6 in enumerate(matches6, start=10):
worksheet.cell(row=idx6, column=8, value=match6)
# 写入****
for idx7, match7 in enumerate(matches7, start=10):
worksheet.cell(row=idx7, column=9, value=match7)
# 写入**
for idx8, match8 in enumerate(matches8, start=10):
worksheet.cell(row=idx8, column=12, value=match8)
# 填写**信息
worksheet.cell(row=5, column=1, value=file_name)
worksheet.cell(row=5, column=6, value=file_name)
worksheet.cell(row=5, column=7, value=file_name)
# 在最后一行填入**信息
# 判断表格有几项
number = len(matches)
# 减去头尾4项文件名,从第10行开始写入
number = number-4+10
# 写入各列固定信息
worksheet.cell(row=number, column=1, value="dt")
worksheet.cell(row=number, column=2, value="**日期")
worksheet.cell(row=number, column=3, value="string")
worksheet.cell(row=number, column=4, value="10")
worksheet.cell(row=number, column=5, value="true")
worksheet.cell(row=number, column=7, value="true")
worksheet.cell(row=number, column=12, value="true")
worksheet.cell(row=number, column=13, value="1")
# 另存为新的Excel表格文件
workbook.save("/Users/alreiters/Desktop/hiveImportTemplate1.xlsx")
#和一开始的“try”构成异常处理结构
except FileNotFoundError:
print("文件未找到,请检查文件路径是否正确。")
except Exception as e:
print(f"发生错误: {e}")
```