本文主要涉及的問題:用ElementTree和XPath讀寫XML文件;解決ElementTree新增元素後再寫入格式不統一的問題;QTableWidget單元格設置控制項 ...
一、項目地址
https://github.com/LinFeng-BingYi/DailyAccountBook
二、新增
1. 解析xml文件
1.1 功能詳述
解析所設計的xml文件格式,並將所得數據存入變數。
→→→點擊查看xml格式←←←
<DailyAccountBook>
<balance>
<fund>
<value>5000.00</value>
<category>0</category>
<fundName>微信零錢</fundName>
</fund>
<fund>
<value>999.00</value>
<category>1</category>
<fundName>中國銀行卡</fundName>
</fund>
<fund>
<value>90.00</value>
<category>2</category>
<fundName>羊城通</fundName>
</fund>
<fund>
<value>1700.07</value>
<category>3</category>
<fundName>支付寶餘額寶</fundName>
</fund>
<fund>
<value>5000.00</value>
<category>4</category>
<fundName>代管存款</fundName>
</fund>
</balance>
<year value="2023">
<month value="09">
<day value="11">
<expenses>
<expense necessity="True" associatedFund="None">
<value>5.00</value>
<category>1</category>
<detail>地鐵</detail>
<describe>早上上班。羊城通卡餘額=100-5=95元</describe>
<from>2</from>
</expense>
<expense necessity="True" associatedFund="None">
<value>5.00</value>
<category>1</category>
<detail>地鐵</detail>
<describe>晚上下班。羊城通卡餘額=95-5=90元</describe>
<from>2</from>
</expense>
<expense necessity="False" associatedFund="None">
<value>1.00</value>
<category>12</category>
<detail>業務手續費</detail>
<describe>微信零錢提現1000的手續費</describe>
<from>0</from>
</expense>
</expenses>
<incomes>
<income associatedFund="4">
<value>3000.00</value>
<category>5</category>
<!-->代管存款的收支,應屬於“非本人相關”類別<-->
<detail>轉賬</detail>
<describe>托管人轉給本人3000,幫忙存放。本人微信零錢餘額=3000+3000=6000元;同時代管存款餘額=2000+3000=5000元</describe>
<to>0</to>
</income>
<income associatedFund="None">
<value>0.07</value>
<category>2</category>
<detail>理財</detail>
<describe>昨日餘額寶收益。餘額=1700+0.07=1700.07</describe>
<to>3</to>
</income>
</incomes>
<movements>
<!-->存款賬戶之間的資金轉移不記錄在收支變化量中,僅修改賬戶餘額。由於兩個賬戶餘額一增一減,而總量不變,記錄下來反而影響個人收支統計<-->
<movement>
<value>999.00</value>
<detail>提現</detail>
<describe>從微信零錢向中國銀行卡提現1000元。完成後微信零錢餘額=6000-999-1=5000;中國銀行卡餘額=0+999=999;被收取0.1%的手續費</describe>
<from>0</from>
<to>1</to>
</movement>
</movements>
<variation>
<fund>
<category>0</category>
<out>1.00</out>
<in>3000.00</in>
</fund>
<fund>
<category>1</category>
<out>0.00</out>
<in>0.00</in>
</fund>
<fund>
<category>2</category>
<out>10.00</out>
<in>0.00</in>
</fund>
<fund>
<category>3</category>
<out>0.00</out>
<in>0.07</in>
</fund>
<fund>
<category>4</category>
<out>0.00</out>
<in>3000.00</in>
</fund>
</variation>
</day>
</month>
</year>
</DailyAccountBook>
解析目的:
- 將
balance
元素中各項fund
子元素存入列表,列表中每一項都是一個代表fund
元素的字典; - 將對應日期的
day
元素中各種動賬類型記錄集合存入字典,該字典內容格式如下:
day_dict = {
'expenses': [expense_dict1, expense_dict2, ...],
'incomes': [income_dict1, ...],
'movements': [movement_dict1, ...],
'variation': [fund_dict1, ...]
}
1.2 代碼實現
解析balance
元素:
def parseBalance(self):
e_balance = self.e_dailyAccountBook.find(".//balance")
balance_list = []
for e_fund in list(e_balance):
balance_dict = {"value": float(e_fund.find('.//value').text),
"category": int(e_fund.find('.//category').text),
"fundName": e_fund.find('.//fundName').text}
balance_list.append(balance_dict)
return balance_list
解析day
元素:
def getSpecificDateElement(self, date_str):
"""
Describe: 根據日期字元串獲取指定的day元素
Args:
date_str: str
格式為"yyyyMMdd"
Returns:
若找到指定日期的元素,則返回Element類型的day元素.
若未找到指定year,則返回int類型的0;若未找到指定month,則返回int類型的1;若未找到指定day,則返回int類型的2。
int型返回值用於控制從何處開始初始化日期元素。
"""
e_year = self.e_dailyAccountBook.find(".//year[@value='{}']".format(date_str[:4]))
if e_year is None:
return 0
e_month = e_year.find(".//month[@value='{}']".format(date_str[4:6]))
if e_month is None:
return 1
e_day = e_month.find(".//day[@value='{}']".format(date_str[6:]))
if e_day is None:
return 2
return e_day
def parseSpecificDateElement(self, date_str):
e_date = self.getSpecificDateElement(date_str)
if isinstance(e_date, int):
print("未找到這一天的數據!")
return None
parse_dict = dict()
for child_node in list(e_date):
print(child_node.tag)
if child_node.tag == 'expenses':
e_expenses = e_date.find(".//expenses")
expenses_list = [self.parseExpense(e_expense) for e_expense in list(e_expenses)]
parse_dict['expenses'] = expenses_list
elif child_node.tag == 'incomes':
e_incomes = e_date.find(".//incomes")
incomes_list = [self.parseIncome(e_income) for e_income in list(e_incomes)]
parse_dict['incomes'] = incomes_list
elif child_node.tag == 'movements':
e_movements = e_date.find(".//movements")
movements_list = [self.parseMovement(e_movement) for e_movement in list(e_movements)]
parse_dict['movements'] = movements_list
elif child_node.tag == 'variation':
e_variation = e_date.find(".//variation")
variation_list = [self.parseVariation(e_fund) for e_fund in list(e_variation)]
parse_dict['variation'] = variation_list
else:
print("未知類型的節點名")
return parse_dict
def parseExpense(self, e_expense):
expense_dict = {
'necessity': True if (e_expense.attrib['necessity'].lower() == 'true') else False,
'value': float(e_expense.find('.//value').text),
'category': int(e_expense.find('.//category').text),
'detail': e_expense.find('.//detail').text,
'describe': e_expense.find('.//describe').text,
'from': int(e_expense.find('.//from').text),
'associatedFund': int(e_expense.attrib['associatedFund']) if (
('associatedFund' in e_expense.attrib) and e_expense.attrib['associatedFund'] != 'None') else None
}
return expense_dict
def parseIncome(self, e_income):
pass
def parseMovement(self, e_movement):
pass
def parseVariation(self, e_fund):
pass
2. 編輯賬本界面-新增行
2.1 功能詳述
在選擇文件或不同的日期後,解析xml文件中對應日期的收支記錄,將其展示在QTableWidget中。此外,表格中最後一列增加兩種操作控制項:
- 對於已存在記錄的行:包含修改、刪除按鈕;
- 對於空白行:包含新增按鈕。點擊新增後,該行變成已存在記錄的行,故而操作控制項也相應地變化,同時表格再新增一行空白行
2.2 代碼實現
def responseSelectedDateChanging(self):
if not self.lineEdit_file_path.text():
print("還未選擇文件!")
return
self.file_processor = AccountBookXMLProcessor(self.lineEdit_file_path.text())
self.file_parse_result = self.file_processor.parseSpecificDateElement(self.dateEdit.text().replace('/', ''))
print(self.file_parse_result)
if self.file_parse_result is None:
self.file_parse_result = {}
if 'expenses' not in self.file_parse_result:
self.file_parse_result['expenses'] = []
if 'incomes' not in self.file_parse_result:
self.file_parse_result['incomes'] = []
if 'movements' not in self.file_parse_result:
self.file_parse_result['movements'] = []
if 'variation' not in self.file_parse_result:
self.file_parse_result['variation'] = []
self.updateExpenseTable(self.file_parse_result['expenses'])
self.updateIncomeTable(self.file_parse_result['incomes'])
self.updateMovementTable(self.file_parse_result['movements'])
def updateExpenseTable(self, expenses_list):
self.tableWidget_expense.setRowCount(0)
self.tableWidget_expense.setRowCount(len(expenses_list)+1)
current_row = 0
for expense_dict in expenses_list:
self.tableWidget_expense.setItem(current_row, 0, QTableWidgetItem(str(expense_dict['necessity'])))
self.tableWidget_expense.setItem(current_row, 1, QTableWidgetItem(str(expense_dict['value'])))
self.tableWidget_expense.setItem(current_row, 2, QTableWidgetItem(str(expense_dict['category'])))
self.tableWidget_expense.setItem(current_row, 3, QTableWidgetItem(str(expense_dict['detail'])))
self.tableWidget_expense.setItem(current_row, 4, QTableWidgetItem(str(expense_dict['describe'])))
self.tableWidget_expense.setItem(current_row, 5, QTableWidgetItem(str(expense_dict['from'])))
self.tableWidget_expense.setItem(current_row, 6, QTableWidgetItem(str(expense_dict['associatedFund'])))
self.tableWidget_expense.setCellWidget(current_row, 7, self.buttonsForExistRow(self.tableWidget_expense))
current_row += 1
self.tableWidget_expense.setItem(current_row, 4, QTableWidgetItem(' '))
self.tableWidget_expense.setCellWidget(current_row, 7, self.buttonsForNewRow(self.tableWidget_expense))
def updateIncomeTable(self, incomes_list):
pass
def updateMovementTable(self, movements_list):
pass
def buttonsForExistRow(self, tableWidget):
widget = QWidget()
# 更新
updateBtn = QPushButton('更新')
updateBtn.clicked.connect(lambda: self.updateTableRow(tableWidget))
# 刪除
deleteBtn = QPushButton('刪除')
deleteBtn.clicked.connect(lambda: self.deleteTableRow(tableWidget))
hLayout = QHBoxLayout(widget)
hLayout.addWidget(updateBtn)
hLayout.addWidget(deleteBtn)
hLayout.setContentsMargins(5, 2, 5, 2)
return widget
def buttonsForNewRow(self, tableWidget):
widget = QWidget()
# 新增
newBtn = QPushButton('新增')
newBtn.clicked.connect(lambda: self.newTableRow(newBtn, tableWidget))
hLayout = QHBoxLayout(widget)
hLayout.addWidget(newBtn)
hLayout.setContentsMargins(5, 2, 5, 2)
return widget
def updateTableRow(self, toggledBtn, tableWidget):
pass
def deleteTableRow(self, toggledBtn, tableWidget):
pass
def newTableRow(self, toggledBtn, tableWidget):
print('觸發了新增按鈕')
# 獲取觸發信號的控制項所在行號
row = tableWidget.indexAt(toggledBtn.parent().pos()).row()
new_data_dict = dict()
if tableWidget == self.tableWidget_expense:
current_column_head = TABLEWIDGET_EXPENSE_COLUMN_HEAD
elif tableWidget == self.tableWidget_income:
current_column_head = TABLEWIDGET_INCOME_COLUMN_HEAD
elif tableWidget == self.tableWidget_movement:
current_column_head = TABLEWIDGET_MOVEMENT_COLUMN_HEAD
else:
print('未知控制項觸發新增按鈕!')
return
# 用新增行數據構建字典
for i in range(tableWidget.columnCount()-1):
new_data_dict[current_column_head[tableWidget.horizontalHeaderItem(i).text()]] = tableWidget.item(row, i).text()
print(new_data_dict)
# 插入新空行
insert_pos = tableWidget.rowCount()
tableWidget.insertRow(insert_pos)
# 新空行"操作"列初始化按鈕
tableWidget.setCellWidget(insert_pos, tableWidget.columnCount()-1, self.buttonsForNewRow(tableWidget))
# 新增行"操作"列初始化按鈕
tableWidget.setCellWidget(insert_pos-1, tableWidget.columnCount()-1, self.buttonsForExistRow(tableWidget))
if tableWidget == self.tableWidget_expense:
# 將"描述"欄位預置空格
tableWidget.setItem(insert_pos, 4, QTableWidgetItem(' '))
# 用新增行的數據組織文件結構
self.file_processor.organizeExpense(new_data_dict, self.dateEdit.text().replace('/', ''))
elif tableWidget == self.tableWidget_income:
tableWidget.setItem(insert_pos, 3, QTableWidgetItem(' '))
self.file_processor.organizeIncome(new_data_dict, self.dateEdit.text().replace('/', ''))
elif tableWidget == self.tableWidget_movement:
tableWidget.setItem(insert_pos, 2, QTableWidgetItem(' '))
self.file_processor.organizeMovement(new_data_dict, self.dateEdit.text().replace('/', ''))
# 將結果文件暫時存放在工作目錄
self.file_processor.writeXMLFile(self.cwd+'\\AccountBookXMLFile.xml')
3. 將新增行寫入xml文件
3.1 功能詳述
用從編輯賬本界面獲取的新增行字典,寫入xml文件。具體過程如下:
- 判斷當前日期是否存在,四種情況:存在年/月/日、只存在年/月、只存在年、均不存在。判斷方法:在方法
getSpecificDateElement(date_str)
中,通過int型返回值作為標誌,再結合方法switch_caseInitStartDate(init_start, date_str)
確定日期元素新增起始點,即判斷從年/月/日開始新增。 - 根據新增行字典內容,修改各存款賬戶的餘額
- 根據新增行字典內容,新增對應動賬記錄
- 若收支記錄關聯了其他存款賬戶,則同時修改關聯賬戶
3.2 代碼實現
def createChildElement(self, e_parent, child_name, child_text, chile_attr=None):
if chile_attr is None:
chile_attr = {}
e_child = et.SubElement(e_parent, child_name, attrib=chile_attr)
e_child.text = child_text
return e_child
def switch_caseInitStartDate(self, init_start, date_str):
# 模擬C++中switch-case控制語句,不使用break的情況
if init_start == 0:
self.createChildElement(self.e_dailyAccountBook, 'year', None, {'value': date_str[:4]})
init_start += 1
if init_start == 1:
e_year = self.e_dailyAccountBook.find(".//year[@value='{}']".format(date_str[:4]))
self.createChildElement(e_year, 'month', None, {'value': date_str[4:6]})
# init_start += 1
# if init_start == 2:
e_year = self.e_dailyAccountBook.find(".//year[@value='{}']".format(date_str[:4]))
e_month = e_year.find(".//month[@value='{}']".format(date_str[4:6]))
e_date = self.createChildElement(e_month, 'day', None, {'value': date_str[6:]})
return e_date
def organizeExpense(self, expense_dict: dict, date_str):
e_date = self.getSpecificDateElement(date_str)
if isinstance(e_date, int):
print("未找到這一天的數據!")
e_date = self.switch_caseInitStartDate(e_date, date_str)
e_expenses = e_date.find(".//expenses") if e_date.find(".//expenses") is not None else self.createChildElement(e_date, 'expenses', None)
e_expense = et.SubElement(e_expenses, 'expense')
self.organizeVariation(expense_dict, e_date)
if 'necessity' in expense_dict:
e_expense.set('necessity', expense_dict['necessity'])
del expense_dict['necessity']
if 'associatedFund' in expense_dict:
e_expense.set('associatedFund', expense_dict['associatedFund'])
del expense_dict['associatedFund']
for key, value in expense_dict.items():
self.createChildElement(e_expense, key, value)
def organizeVariation(self, change_dict, e_date):
e_variation = e_date.find(".//variation") if e_date.find(".//variation") is not None else self.createChildElement(e_date, 'variation', None)
if 'from' in change_dict:
if e_variation.find(".//fund[category='{}']".format(change_dict['from'])) is None:
e_fund = self.createChildElement(e_variation, 'fund', None)
self.createChildElement(e_fund, 'category', change_dict['from'])
self.createChildElement(e_fund, 'out', '0.0')
self.createChildElement(e_fund, 'in', '0.0')
e_fund_variety = e_variation.find(".//fund[category='{}']/out".format(change_dict['from']))
e_fund_variety.text = str((Decimal(e_fund_variety.text) + Decimal(change_dict['value'])).quantize(Decimal('0.00')))
self.modifyBalance(change_dict['from'], Decimal(change_dict['value'])*(-1))
self.organizeAssociatedFund(e_variation, change_dict, 'from')
if 'to' in change_dict:
if e_variation.find(".//fund[category='{}']".format(change_dict['to'])) is None:
e_fund = self.createChildElement(e_variation, 'fund', None)
self.createChildElement(e_fund, 'category', change_dict['to'])
self.createChildElement(e_fund, 'out', '0.0')
self.createChildElement(e_fund, 'in', '0.0')
e_fund_variety = e_variation.find(".//fund[category='{}']/in".format(change_dict['to']))
e_fund_variety.text = str((Decimal(e_fund_variety.text) + Decimal(change_dict['value'])).quantize(Decimal('0.00')))
self.modifyBalance(change_dict['to'], Decimal(change_dict['value']))
self.organizeAssociatedFund(e_variation, change_dict, 'to')
def modifyBalance(self, fund_category, increment_value: Decimal):
"""
Describe:
Args:
fund_category: int or str
存款賬戶類型
increment_value: Decimal
餘額增量,為正或負
"""
e_value = self.e_dailyAccountBook.find(".//balance").find(".//fund[category='{}']".format(fund_category)).find(".//value")
e_value.text = str((Decimal(e_value.text) + increment_value).quantize(Decimal('0.00')))
def organizeAssociatedFund(self, e_variation, change_dict, from_or_to):
print(change_dict['associatedFund'])
if 'associatedFund' in change_dict and change_dict['associatedFund'] != 'None':
print('執行了associatedFund,操作為', from_or_to)
if e_variation.find(".//fund[category='{}']".format(change_dict['associatedFund'])) is None:
e_associated_fund = self.createChildElement(e_variation, 'fund', None)
self.createChildElement(e_associated_fund, 'category', change_dict['associatedFund'])
self.createChildElement(e_associated_fund, 'out', '0.0')
self.createChildElement(e_associated_fund, 'in', '0.0')
if from_or_to == 'from':
e_fund_variety = e_variation.find(".//fund[category='{}']/out".format(change_dict['associatedFund']))
flag = -1
elif from_or_to == 'to':
e_fund_variety = e_variation.find(".//fund[category='{}']/in".format(change_dict['associatedFund']))
flag = 1
else:
print('未知的收支動作!')
return
e_fund_variety.text = str((Decimal(e_fund_variety.text) + Decimal(change_dict['value'])).quantize(Decimal('0.00')))
self.modifyBalance(change_dict['associatedFund'], Decimal(change_dict['value'])*flag)
三、開發總結
1. ElementTree模塊基本使用方法
//Element基本構成
<tag attrib1=1>text</tag>tail
<元素名 屬性1=1>文本</元素名>尾部
//解析文件
xml_tree = ElementTree.parse("AccountBookFile.xml")
//獲取根元素
root = xml_tree.getroot()
//查找子元素:按元素名查找,返回匹配到的第一個元素
//舉例:查找root元素的第一個year子元素
//情景含義:找到xml文件的第一個年份的動賬記錄
year = root.find(".//year")
//查找子元素:按元素名查找,返回所有匹配到的元素
//舉例:查找year元素的所有month元素
//情景含義:找到年份中所有月份的動賬記錄
month_list = year.findall(".//month")
//查找子元素:查找具有特定屬性值的子元素
//舉例:查找month元素直屬子元素中,符合該條件的子元素——具有屬性value=10
//情景含義:找到第一個月份中10號日期的動賬記錄
day = month_list[0].find(".//day[@value='10']")
//查找子元素:查找具有特定子元素值的子元素
//舉例:查找variation元素直屬子元素中,符合該條件的子元素——包含文本值為0的category子元素
//情景含義:找到10號微信零錢(category=0)的收支總和
variation = day.find(".//variation")
fund = variation.find(".//fund[category='0']")
//查找子元素:查找具有特定屬性值,同時有特定子元素值的子元素
//舉例:查找expenses元素直屬子元素中,符合該條件的子元素——具有屬性necessity=True,同時包含文本值為0的category子元素
//情景含義:找到10號基本開支中用於飲食(category=0)的花銷記錄
expenses = day.find(".//expenses")
expense = expenses.find(".//expense[@necessity='True'][category='0']")
//查找非直屬子元素:根據路徑查找子元素
//舉例:查找root元素下,具有屬性value=2023的year元素下,具有屬性value=09的month元素下,具有屬性value=11的day元素下,所有expenses元素下,包含文本值為1的category子元素的expense元素
//情景含義:找到2023/09/11這一天用於出行(category=1)的花銷記錄
expense = root.findall(".//year[@value='2023']/month[@value='09']/day[@value='11']/expenses/expense[category='1']")
//創建子元素
//舉例:為e_parent元素創建子元素e_child,子元素名(tag)為child_name,子元素屬性(attrib)鍵值對包含在字典attr_dict中,子元素文本值(text)為child_text
e_child = ElementTree.SubElement(e_parent, child_name, attrib=attr_dict)
e_child.text = child_text
//添加子元素
//舉例:將現有的e_child元素設置為e_parent元素的子元素
e_parent.append(e_child)
//修改element元素的名稱和文本
element.tag = "new_tag"
element.text = "new_text"
//新增或修改element元素中名為key的屬性(attrib本質上是字典)
element.attrib[key] = "new_value"
//刪除element元素中名為key的屬性,並返回對應的值value。若不存在該屬性,則返回defalt_value
value = element.attrib.pop(key, defalt_value)
//寫入xml文件
xml_tree.write(file_path, encoding='utf-8', xml_declaration=True)
2. 修改xml文件後,出現原有內容與新增內容格式不一致的情況
參考:https://blog.csdn.net/u012692537/article/details/101395192
通過該函數美化一下,再調用write寫入,問題解決
def pretty_xml(element, indent, newline='\n', level=0): # elemnt為傳進來的Elment類,參數indent用於縮進,newline用於換行
if element: # 判斷element是否有子元素
if (element.text is None) or element.text.isspace(): # 如果element的text沒有內容
element.text = newline + indent * (level + 1)
else:
element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * (level + 1)
# else: # 此處兩行如果把註釋去掉,Element的text也會另起一行
# element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * level
temp = list(element) # 將element轉成list
for sub_element in temp:
if temp.index(sub_element) < (len(temp) - 1): # 如果不是list的最後一個元素,說明下一個行是同級別元素的起始,縮進應一致
sub_element.tail = newline + indent * (level + 1)
else: # 如果是list的最後一個元素, 說明下一行是母元素的結束,縮進應該少一個
sub_element.tail = newline + indent * level
pretty_xml(sub_element, indent, newline, level=level + 1) # 對子元素進行遞歸操作
3. QTableWidget單元格內置控制項
核心方法:
def setCellWidget(row: int, column: int, widget: QWidget) -> None
獲QTableWidget中發出信號的控制項所在行號的槽函數:
def get_triggeredObj_pos():
triggeredObj = self.sender() # 獲取信號發出者
# 假設層次關係為triggeredObj放在widget中,而widget放在QTableWidget的單元格中
widget = triggeredObj.parent()
table_widget = widget.parent()
row = table_widget.indexAt(widget.pos()).row()
print(f"The row number of the button is {row}")
本文來自博客園,作者:林風冰翼,轉載請註明原文鏈接:https://www.cnblogs.com/LinfengBingyi/p/17716008.html