当前位置:网站首页>常用模块封装-csv文件操作封装
常用模块封装-csv文件操作封装
2022-08-10 05:35:00 【蓝尼亚】
csv库常用封装 import csv def csv_write_dict(path, fieldnames, dic_row_list): """ 字典形式写入csv文件 :param fieldnames: [] :param dic_row_list: [{},{}] :return: """ with open(path, 'w', newline='') as csvfile: # 构建字段名称,key writer = csv.DictWriter(csvfile, fieldnames=fieldnames) # 写入字段名作表头 writer.writeheader() # 多行写入 writer.writerows(dic_row_list)def csv_write_rows(self, row_list): """ 多行写入 :param row_list: [[],[],[]] :return: """ # 按行写入 with open(self.path, 'wb') as f: f.write(codecs.BOM_UTF8) # 防止输出为乱码 f_csv = csv.writer(f) f_csv.writerows(row_list)def csv_read_dict(path): # 以列表形式,读取csv文件 path = r'E:\huangrong\code\python\TestEnvironmentalGovernance\file\data.log' with open(path, 'r') as f: r_csv = csv.reader(f) li = [row for row in r_csv] print(li) return li if __name__ == '__main__': path = r'E:\huangrong\code\learning\code\ApiTest\file\test.csv' fieldnames = ['first_name', 'last_name'] dic_row_list = [{'first_name': 'Baked', 'last_name': 'Beans'}, {'first_name': 'Lovely', 'last_name': 'Spam'}] # csv_write_dict(path, fieldnames, dic_row_list) csv_read_dict(path)
参考:
边栏推荐
- 【简易笔记】PyTorch官方教程简易笔记 EP2
- LeetCode 292.Nim 游戏(简单)
- R语言聚类分析——代码解析
- el-dropdown drop-down menu style modification, remove the small triangle
- 力扣——省份数量
- generic notes()()()
- pytorch-06.逻辑斯蒂回归
- The way for programmers to make money from a sideline business and increase their monthly income by 20K
- Consensus calculation and incentive mechanism
- Decentralized and p2p networks and traditional communications with centralization at the core
猜你喜欢
随机推荐
STM32单片机OLED经典2048游戏单片机小游戏
离散数学的学习记录
一个基于.Net Core跨平台小程序考试系统
LeetCode 1351.统计有序矩阵中的负数(简单)
LeetCode 剑指offer 10-I.斐波那契数列(简单)
LeetCode 292.Nim 游戏(简单)
【图像识别】训练一个最最简单的AI使其识别Vtuber
The submenu of the el-cascader cascade selector is double-clicked to display the selected content
pytorch-10. Convolutional Neural Networks
Notes for RNN and Decision Tree
Tensorflow 2.0 使用流程详解
Notes for Netual Network
pytorch-11. Convolutional Neural Network (Advanced)
优先队列
A little knowledge point every day
二维卷积定理的验证(上)
新建STM32F407ZG Keil5项目
PyTorch之CV
STM32单片机RGB红蓝调光植物补光系统红光蓝光PWM调色调节亮度
Notes for RNN