我想写个工作用的工具,读取LOG文本,处理得到想要的结果,输出到文本框,通过getting文本框内容读写到EXCEL文件里,结果发生错误
def read_log(self):
global bsc, bcf, bcf_state, bts, bts_state, bts_name
self.file_path = filedialog.askopenfilename()
if self.file_path is None: # fixed BUG 1
return
self.file_text1.insert("insert", self.file_path)
file_opened = open(self.file_path) # 不要用关键字file做变量名
a_list = []
counts = 0
str1 = ""
# file_name = os.path.basename(self.file_path)
for each_line in file_opened:
a_list.append(each_line)
if "FlexiBSC" in each_line:
bsc = each_line[9:19].replace("\n", "")
if "BCF-" in each_line:
bcf = each_line[4:8]
bcf_state = each_line[15:21].replace("\n", "")
if len(bcf_state) < 6:
bcf_state += (6 - len(bcf_state)) * " "
if " BTS-" in each_line:
bts = each_line[5:9]
bts_state = each_line[15:21].replace("\n", "")
bts_name = a_list[counts - 1].replace("\n", "")
if len(bts_state) < 6:
bts_state += (6 - len(bts_state)) * " "
if len(bts_name) < 20:
bts_name += (20 - len(bts_name)) * " "
if " TRX-" in each_line:
trx = each_line[6:9]
trx_state = each_line[15:21].replace("\n", "")
if len(trx_state) < 6:
trx_state += (6 - len(trx_state)) * " "
str1 += (
"退服载频:"
+ bsc
+ " BCF-"
+ bcf
+ " "
+ bcf_state
+ " BTS-"
+ bts
+ " "
+ bts_state
+ " "
+ bts_name
+ "TRX-"
+ trx
+ " "
+ trx_state
+ "\n"
)
counts += 1
self.file_text2.insert("insert", str1)
def save_reslut(self):
counts = 1
wb = Workbook()
ws = wb.active
ws.title = "result"
ws.append(["BSC", "BCF", "BCF-STATE", "BTS", "BTS-STATE", "NAME", "TRX", "TRX-STATE"])
savefile_path = filedialog.asksaveasfilename()
if savefile_path is None: # fixed BUG 1
return
#self.savefile_opened = open(self.savefile_path, 'w')
for each in ((self.file_text2.get("1.0", "end")).split(str="\n")):
counts += 1
ws.cell(row=counts, column=1).value = each[5:14]
wb.save(savefile_path)
#self.savefile_opened.write(self.file_text2.get("1.0", "end"))
#self.savefile_opened.close()
messagebox.showinfo(title='提示', message='保存成功! ')
E:\python_obj\Scripts\python.exe C:/Users/Lenovo/PycharmProjects/pythonProject1/main.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call
return self.func(*args)
File "C:\Users\Lenovo\PycharmProjects\pythonProject1\main.py", line 121, in read_log
file_opened = open(self.file_path) # 不要用关键字file做变量名
File "E:\python_obj\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "E:\python_obj\lib\site-packages\openpyxl\reader\excel.py", line 124, in init
self.archive = _validate_archive(fn)
File "E:\python_obj\lib\site-packages\openpyxl\reader\excel.py", line 94, in _validate_archive
raise InvalidFileException(msg)
openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support .log file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
进程已结束,退出代码为 0
报错是指openpyxl用load_workbook不能读取log格式文件,支持的文件格式为xlsx,.xlsm,.xltx,.xltm。因此可考虑在代码中将log文件用pandas转换并保存为xlsx文件,再用代码中的方法读取处理。