python">import json
import subprocess
import re
import openpyxl
def logcat(excel_path, check_re):
"""
查看 安卓手机日志信息
:param excel_path: excel的路径信息,标题行字段
:param check_re: 过滤当前日志的正则表达式(之后记得优化)
:return:
"""
workbook = openpyxl.load_workbook(excel_path)
sheet = workbook.active
first_row_data = []
for cell in sheet[1]:
first_row_data.append(cell.value)
subprocess.run("adb logcat -c", shell=True, check=True)
adb_command = "adb logcat"
json_pattern = re.compile(r'\{.*\}')
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.append(first_row_data)
sheet.column_dimensions['A'].width = 23
sheet.column_dimensions['B'].width = 23
try:
process = subprocess.Popen(adb_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line_bytes = process.stdout.readline()
if not line_bytes:
break
line = line_bytes.decode('utf-8', errors='ignore').strip()
match = json_pattern.search(line)
if match:
json_content = match.group()
if re.search(check_re, json_content):
print(json_content)
json_data = json.loads(json_content)
temp = []
for cell in first_row_data:
temp.append(json_data[cell])
sheet.append(temp)
process.wait()
except Exception as e:
print(f"{e}")
finally:
workbook.save(excel_path)
workbook.close()
if __name__ == '__main__':
logcat("example.xlsx", r'status.*orderId_.*')