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_.*')

- 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