当前位置: 首页 > news >正文

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)
http://www.lryc.cn/news/334957.html

相关文章:

  • 04---webpack编写可维护的构建配置
  • 【云计算】云数据中心网络(一):VPC
  • 自动驾驶中的多目标跟踪_第一篇
  • AI爆款文案 巧用AI大模型让文案变现插上翅膀
  • Python入门的60个基础练习(一)
  • 微软云学习环境
  • 大厂面试:找出数组中第k大的数的最佳算法
  • 爬取高校专业信息的Python爬虫简介与实践
  • redis 集群模式(redis cluster)介绍
  • python实现网络爬虫
  • LeetCode 836. 矩形重叠
  • 为说阿拉伯语的国家进行游戏本地化
  • 【Python系列】读取 Excel 第一列数据并赋值到指定列
  • 二叉树——存储结构
  • LangChain - OpenGPTs
  • pe格式从入门到图形化显示(四)-节表
  • 路由策略与路由控制之双点双向重发布(OSPF-ISIS)实验
  • 9proxy—数据采集工具全面测评
  • 上海晶珩树莓派工业智能机械臂,亮相2024年embedded world博览会!
  • 蓝桥杯——求和
  • 设计模式:责任链模式示例
  • SpringBoot快速入门笔记(4)
  • GoPro相机使用的文件格式和频率
  • Redis Stack 安装部署
  • 【经典算法】LeetCode 5: 最长回文子串(Java/C/Python3实现含注释说明,Medium)
  • 39.Python从入门到精通—parseString 方法 Python 解析XML实例 使用xml.dom解析xml
  • 【蓝桥杯第九场小白赛】(部分)
  • 【Linux】Supervisor 基础
  • 48 全连接卷积神经网络 FCN【动手学深度学习v2】
  • pytorch中的nn.MSELoss()均方误差损失函数