Python 标准库 csv —— csv 文件的读写
2017-05-12 23:20
471 查看
csv 文件,逗号分割文件。
注意文件的打开模式,如果
1. 写入并生成 csv 文件
注意这里是写入并生成,而非创建并写入,也即可自动创建一个不存在的 csv 文件。import csv with open('test.csv', 'w') as f: writer = csv.writer(f) # 写入表头,表头是单行数据 writer.writerow(['name', 'age', 'tel']) data = [ ('zhangsan', 20, 'xxxx'), ('lisi', 22, 'xxxx') ] # 写入这些多行数据 writer.writerows(data)
注意文件的打开模式,如果
with open('', 'wb')的方式打开,向其中写入字符内容时,很容易出现
TypeError: a bytes-like object is required, not 'str' when writing to a file的类型错误。详细内容见python 3.5: TypeError: a bytes-like object is required, not ‘str’ when writing to a file
相关文章推荐
- Python读写csv文件
- Python读写csv文件
- Python读写csv文件
- python数据分析之(4)读写数据文件CSV,EXCEL等
- Python读写csv文件
- python 读写 csv文件
- python使用csv模块读写csv文件
- python 读写csv文件(一)
- python 读写CSV文件
- python读写csv_xml_json配置文件
- Python读写csv文件
- python读写word、excel、csv、json文件
- python/机器学习:读写机器学习中常用的.CSV数据文件t
- Python读写csv文件
- python 读写csv格式文件
- Python CSV文件处理/读写
- python读写csv文件
- (4)Python读写csv文件
- python简单的读写csv文件
- python3使用csv模块读写csv文件