Pandas追加写入文件的时候写入到了第一行

# 原代码
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)

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 21:10:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 21:10:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 21:10:02       87 阅读
  4. Python语言-面向对象

    2024-04-09 21:10:02       96 阅读

热门阅读

  1. 程序员如何利用副业实现财务自由

    2024-04-09 21:10:02       36 阅读
  2. C++逻辑运算符

    2024-04-09 21:10:02       34 阅读
  3. Shell编程训练2

    2024-04-09 21:10:02       39 阅读
  4. C++:Stmt预处理SQL与大文件存取(五)

    2024-04-09 21:10:02       39 阅读
  5. 前端实现下载的2中方法(个人总结)

    2024-04-09 21:10:02       35 阅读
  6. 嵌入式之关键词篇(一)

    2024-04-09 21:10:02       31 阅读
  7. shell命令行中脚本特殊注释指定脚本解释器

    2024-04-09 21:10:02       34 阅读
  8. Python入门的60个基础练习(一)

    2024-04-09 21:10:02       42 阅读
  9. Docker容器嵌入式开发:在Ubuntu上配置Hive

    2024-04-09 21:10:02       43 阅读
  10. 每日一题 第七十九期 Codeforces Global Round 25

    2024-04-09 21:10:02       39 阅读
  11. 动态指定easyui的datagrid的url

    2024-04-09 21:10:02       42 阅读