csv文件操作

在爬虫中经常会遇到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.基本的读取文件的方式

import csv
csv_reader=csv.reader(open('taxi.csv',encoding='utf-8'))
for row in csv_reader:
    print(row)
#taxi.csv最好放在同一目录下
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

2.读取文件中的某一列以及多列

import csv
with open('taxi1.csv',encoding='utf-8') as csvfile:
    reader=csv.reader(csvfile)
    column=[row[2] for row in reader]
    print(column)


####方法2
data_x= pd.read_csv(filepath_or_buffer = 'taxi1.csv', sep = ',')["lo"].values
data_y= pd.read_csv(filepath_or_buffer = 'taxi1.csv', sep = ',')["la"].values
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.读取文件的某一行

import csv
with open('taxi1.csv',encoding='utf-8') as csvfile:
    reader=csv.reader(csvfile)
    for i,rows in enumerate(reader):
        if i==0:
            row=rows
    print(row)
###
['id', 'dest_no', 'lo', 'la', 'gps_time', 'status', 'speed', 'vehicle_type', 'taxi_no_color', 'dir', 'create_time']
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.读取文件的行数

import csv
a=open("taxi1.csv","r")
b=len(a.readline())
print(b)

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值