当前位置:网站首页>csv模块
csv模块
2022-08-07 09:52:00 【HHYZBC】
csv模块实现以CSV格式读取和写入表格数据的类。该模块是python的内置模块,使用时直接引用即可。
写入
创建一个写入对象
w = csv.writer(f)f是需要写入的文件句柄,所有的写入操作都是在这个写入对象上完成。
写入单行数据
w.writerow([1001, "北京", ""])写入多行数据
w.writerows([[1001, "北京", ""], [1002, "湖南", ""], [1003, "湖北", ""]])同时,该模块也支持使用字典的方式进行写入数据
# 表头
headers = ["学号", "姓名", "分数"]
# 写入的内容
rows = [{"学号": "2022021", "姓名": "张三", "分数": "98"},
{"学号": "2022022", "姓名": "张一", "分数": "98"},
{"学号": "2022023", "姓名": "张二", "分数": "98"}]
# 创建字典的写入方式,f为文件句柄,headers为表头
ws = csv.DictWriter(f, headers)
# 写入表头
ws.writeheader()
# 写入数据
ws.writerows(rows)读取
import csv
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)边栏推荐
- 世界上最大的开源基金会 Apache 是如何运作的?
- The principle and source code of redis-sentinel sentinel principle and source code analysis (on)
- For high-performance, ultra-large-scale model training, this combination "debuts"
- 进程与线程的区别与联系
- redis的原理和源码-redis的六种数据类型基本介绍:string、hash、list、set、zset、stream
- window.requestAnimationFrame Web3D渲染帧率控制
- thinkphp 6.x arbitrary file write vulnerability
- LeetCode #100. 相同的树
- 【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
- How does Apache, the world's largest open source foundation, work?
猜你喜欢
随机推荐
For high-performance, ultra-large-scale model training, this combination "debuts"
npm 发布问题
【位带操作对寄存器赋值】基于ADuCM4050的GPIO复用模式初始化
进程的调度
Selection sort (simple selection sort and heap sort)
The principle and source code of redis-server introduction and source code analysis (execution process of command request, server initialization process)
During the meeting, I was scolded by the leader for more than 10 minutes. The scolding made me almost cry, and my voice was shaking.
开会时被领导骂了10多分钟,骂得我快哭了,说话声音都在发抖
基于ABP和Magicodes实现Excel导出操作
4.2 实现注册与登录模块
360企业安全云亮相2022全球数字经济大会 夯实中小微企业数字化底座
pytorch中的优化器
基于FME开发的几何数据拓扑错误自动化处理软件
自定义推送铃声插件遇到的问题
Redis只能做缓存?太out了!
thinkphp 6.x arbitrary file write vulnerability
Knowledge of Node
Node的知识理解
大厂外包,值得拥有吗?
MES生产管理系统是什么?有ERP系统了为什么还要上









