# 原代码
def find_money(file_path, account, b_account, money, type_word, time):
file = pd.read_excel(file_path)
with open('money.csv', 'a', newline='', encoding='utf-8') as f:
for i in file.index:
···省略中间的代码···
if ···省略中间的代码···:
file.loc[[i]].to_csv(f,index=False)
find_same(file, account, b_account, money, type_word, time)
def find_same(file, account, b_account, money, type_word, time):
···省略中间的代码···
with open('money.csv', 'a', newline='', encoding='utf-8') as f:
for i in file.index:
···省略中间的代码···
if ···省略中间的代码···
file.loc[[i]].to_csv(f,header=False, index=False)
在处理数据的时候,遇到了不管怎样修改都无法将数据追加写入到最后一行的位置上,在询问各大AI大模型和翻阅Google无果后,找到了解决方法
即:在with open()之后,退出with open()再运行接下来的操作,方可解决
# 修改之后的代码
def find_money(file_path, account, b_account, money, type_word, time):
file = pd.read_excel(file_path)
for i in file.index:
···省略中间的代码···
if ···省略中间的代码···:
with open('money.csv', 'a', newline='', encoding='utf-8') as f:
file.loc[[i]].to_csv(f,index=False)
find_same(file, account, b_account, money, type_word, time)
def find_same(file, account, b_account, money, type_word, time):
···省略中间的代码···
for i in file.index:
···省略中间的代码···
if ···省略中间的代码···
with open('money.csv', 'a', newline='', encoding='utf-8') as f:
file.loc[[i]].to_csv(f,header=False, index=False)