Python读写csv文件
2016-10-17 19:45
435 查看
1. 写入并生成csv文件
代码:
# coding: utf-8
import csv
csvfile = file('csv_test.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['姓名', '年龄', '电话'])
data = [
('小河', '25', '1234567'),
('小芳', '18', '789456')
]
writer.writerows(data)
csvfile.close()
wb中的w表示写入模式,b是文件模式
写入一行用writerow
多行用writerows
2. 读取csv文件
代码:
# coding: utf-8
import csv
csvfile = file('csv_test.csv', 'rb')
reader = csv.reader(csvfile)
for line in reader:
print line
csvfile.close()
运行结果:
root@he-desktop:~/python/example# python read_csv.py
['\xe5\xa7\x93\xe5\x90\x8d', '\xe5\xb9\xb4\xe9\xbe\x84', '\xe7\x94\xb5\xe8\xaf\x9d']
['\xe5\xb0\x8f\xe6\xb2\xb3', '25', '1234567']
['\xe5\xb0\x8f\xe8\x8a\xb3', '18', '789456']
代码:
# coding: utf-8
import csv
csvfile = file('csv_test.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['姓名', '年龄', '电话'])
data = [
('小河', '25', '1234567'),
('小芳', '18', '789456')
]
writer.writerows(data)
csvfile.close()
wb中的w表示写入模式,b是文件模式
写入一行用writerow
多行用writerows
2. 读取csv文件
代码:
# coding: utf-8
import csv
csvfile = file('csv_test.csv', 'rb')
reader = csv.reader(csvfile)
for line in reader:
print line
csvfile.close()
运行结果:
root@he-desktop:~/python/example# python read_csv.py
['\xe5\xa7\x93\xe5\x90\x8d', '\xe5\xb9\xb4\xe9\xbe\x84', '\xe7\x94\xb5\xe8\xaf\x9d']
['\xe5\xb0\x8f\xe6\xb2\xb3', '25', '1234567']
['\xe5\xb0\x8f\xe8\x8a\xb3', '18', '789456']
相关文章推荐
- python 读写csv格式文件
- Python读写csv文件
- Python读写csv文件
- python中文件的读写——read_csv()读取文件
- python简单的读写csv文件
- python 读写 csv文件
- python使用csv模块读写csv文件
- python 读写csv文件(一)
- python 读写CSV文件
- python数据分析之(4)读写数据文件CSV,EXCEL等
- Python读写csv文件
- python读写word、excel、csv、json文件
- python/机器学习:读写机器学习中常用的.CSV数据文件t
- python3使用csv模块读写csv文件
- Python CSV文件处理/读写
- Python读写csv文件
- python读写csv文件
- Python读写csv文件
- Python——csv txt文件读写
- Python读写csv文件