需要提取.c中的配置数据,用以他用,c文件的内容简单举例如下:
#define hello 0
/
SPICMD(0xFB);
SPIDAT(0x00);
SPIDAT(0x01);
//SPICMD(0xFB);
//SPIDAT(0x02);
//SPIDAT(0x03);
SPICMD(0xFB); //SPIDAT(0x04); SPIDAT(0x05);
SPIDAT(0x08); SPIDAT(0x09);/*
SPICMD(0xFB);
SPIDAT(0x06);
SPIDAT(0x07);
*/
目的是删掉红色部分内容,保留黑色部分数据,并格式化去空格、Tab以及分行。
中间有用好多方法,最后询问了CSDN的Gpt给的建议,灵光一闪,用re简单处理好了。
openfile.py
import sys
import re
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog, QTextEdit
from PySide6.QtGui import QAction
from openfile_ui import Ui_Form
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton.released.connect(self.file_open)
def file_open(self):
file_name, _ = QFileDialog.getOpenFileName(self, '打开文件', '', 'C files (*.c)')
if file_name:
# with open(file_name, 'r', encoding='utf-8') as file:
file = open(file_name,'r', encoding='utf-8')
content = file.read() #as string
regex_case = re.compile('.*SPICMD.*')
regex_case1 = re.compile('.*SPIDAT.*')
regex_case2 = re.compile('//.*') #找//
regex_case3 = re.compile(r'/\*.*?\*/', re.DOTALL) #找/* */
text_no_comment1 = re.sub(regex_case2, '', content)# del //后面内容
content = re.sub(regex_case3, '', text_no_comment1)# del /* */之间内容
content = content.replace(' ','').replace(' ','').replace(';',';\n')
# print(content)
tmp_list = content.split('\n')
content_list = [i for i in tmp_list if i != ""]
# print(content_list)
str1 = ''
for line in content_list:
regex_case = re.compile('.*SPICMD.*')
regex_case1 = re.compile('.*SPIDAT.*')
found_case = regex_case.search(line)
found_case1 = regex_case1.search(line)
if found_case or found_case1:
# print(line)
str1 = str1 + line + '\n'
self.ui.textEdit.setPlainText(str1)
file.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
openfile_ui.py
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'openfile_ui.ui'
##
## Created by: Qt User Interface Compiler version 6.4.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QPushButton, QSizePolicy, QTextEdit,
QVBoxLayout, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(404, 271)
self.verticalLayoutWidget = QWidget(Form)
self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget")
self.verticalLayoutWidget.setGeometry(QRect(0, 0, 401, 271))
self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.textEdit = QTextEdit(self.verticalLayoutWidget)
self.textEdit.setObjectName(u"textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.pushButton = QPushButton(self.verticalLayoutWidget)
self.pushButton.setObjectName(u"pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.pushButton.setText(QCoreApplication.translate("Form", u"&Open", None))
# retranslateUi
运行结果如下:
如此获得的数据,再进行二次提炼发送串口或者其他用途。