在爬虫中经常会遇到csv文件的操作,闲余时间,做几个demo练手。
import csv
# 注意,读取csv文件时放在同一层级目录下"""
从列表读取csv文件
"""
# 读取csv文件方式一:
csvfile = open("data.csv","r")
# 返回的是迭代类型
reader = csv.reader(csvfile)
data1 = []
for item in reader:
# print(item)
data1.append(item)
print(data1)
# 关闭
csvfile.close()
# 读取csv文件方式二:
with open("data.csv","r")as csvfile:
# 读取csv文件,返回的是迭代类型
reader2 = csv.reader(csvfile)
for item2 in reader2:
print(item2)
# 关闭
csvfile.close()
"""
从列表写入csv文件
"""
# 设置newline,否则两行之间会空一行
csvfile2 = open("csvfile2.csv", "w", newline="")
writer = csv.writer(csvfile2)
m = len(data1)
for i in range(m):
writer.writerrow(data1[i])
csvfile2.close()
"""
从字典写入csv文件
"""
dic = {'张三':123, '李四':456, '王麻子':789}
csvFile3 = open('csvFile3.csv','w', newline='')
writer2 = csv.writer(csvFile3)
for key in dic:
writer2.writerow([key, dic[key]])
csvFile3.close()
1.基本的读取文件的方式
- 1
- 2
- 3
- 4
- 5
2.读取文件中的某一列以及多列
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.读取文件的某一行
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
4.读取文件的行数