python CSV模块使用笔记
来源:互联网 发布:淘宝客三段式pid 编辑:程序博客网 时间:2024/05/21 09:02
import csv
csv.reader:
reader(csvfile[, dialect=’excel’][, fmtparam])
reader = csv.reader(file('glucoseData.csv', 'rb')) for line in reader: print line >>['l490', 'l660', 'l730', 'l850', 'l930', 'glucose', 'glucose_level'] ['0.221', '0.329', '0.323', '0.443', '0.355', '8.1', '3'] ['0.21', '0.328', '0.322', '0.438', '0.371', '7.6', '3'] ['0.203', '0.318', '0.309', '0.387', '0.371', '7.4', '3'] ['0.2', '0.289', '0.3', '0.367', '0.297', '5.9', '2']
csv.writer
writer(csvfile[, dialect=’excel’][, fmtparam])
writer = csv.writer(file('your.csv', 'wb')) writer.writerow(['Column1', 'Column2', 'Column3']) lines = [range(3) for i in range(5)] for line in lines: writer.writerow(line)
DictWriter
可以把json格式的字典保存成csv
rows = [{'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'}, {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'}, {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'}, {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'}, {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'}] # 这样就可以直接调用DictWriter.writerows方法来处理了: import csv fieldnames = ['Column1', 'Column2', 'Column3', 'Column4'] dict_writer = csv.DictWriter(file('your.csv', 'wb'), fieldnames=fieldnames) dict_writer.writerow(fieldnames) # CSV第一行需要自己加入 dict_writer.writerows(rows) # rows就是表单提交的数据
结合with open as 使用
确保文件使用后关闭
with open(file('glucoseData.csv', 'rb')) as csvFile: reader = csv.reader(csvFile) for line in reader: print line
0 0
- python CSV模块使用笔记
- python csv模块使用
- python csv模块使用
- python使用csv模块读写csv文件
- 【Python】 csv模块的使用
- python csv模块的使用
- python csv模块使用实例
- 【Python】 csv模块的使用
- Python csv模块的使用
- Python csv模块的使用
- 【Python】 csv模块的使用
- 【Python】 csv模块的使用
- 【Python】 csv模块的使用
- Python csv模块的使用
- Python-使用CSV模块进行文件处理
- Python csv 模块
- Python的CSV模块
- Python csv模块
- io流操作之随机访问文件流代码
- 福昕PDF阅读器 Foxit PDF Reader 中文绿色版
- 字符串,那些你不知道的事
- js判断安卓、ios、还是pc端
- Hadoop 2.0 – HA功能中ZKFC对NN状态的控制
- python CSV模块使用笔记
- HttpUrlConnection
- winSCP无法连接Linux
- POJ2251 Dungeon Master BFS基础
- 发传单的人
- 集合 collection
- LeetCode 235:Lowest Common Ancestor of a Binary Search Tree
- minicom 工具使用
- Nuget如何管理我们的类库?