python3处理csv文件

python3处理csv文件[Python基础]

1. 基础语句

1.1 文件的读取

 如果需要读取一行数据如下表1所示,那么需要读取域名下面的数据,便使用如下代码:

python">with open("A.csv","rb") as csvfile:

reader = csv.reader(csvfile)

rows = [row for row in reader]

 其中每一个row就是一行["121.241.244.92", "known attacker", ""blocklist.de (+dataplane.org,greensnow.co,rulez.sk,rutgers.edu)""],对应的row[0]就是121.241.244.92。

那么读取某一列就很容易了,取出每一row里面的第0个元素组成colum,便是下面的语句可以取得第0列 column0 = [row[0] for row in reader] 。而取得某一行便相对复杂一些,代码如下:

import csv

with open("A.csv","rb") as csvfile:

reader = csv.reader(csvfile)

for i,rows in enumerate(reader):

if i == 2:

row = rows

 上面的代码中,运用到了enumerate函数,enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。即对于一个列表中每一个元素添加一个索引。通过索引选取对于的行。

seasons = ["Spring", "Summer", "Fall", "Winter"]

list(enumerate(seasons))

[(0, "Spring"), (1, "Summer"), (2, "Fall"), (3, "Winter")]

reader列表

[["121.241.244.92", "known attacker", ""blocklist.de (+dataplane.org,greensnow.co,rulez.sk,rutgers.edu)""],

["121.32.150.82", "known attacker", ""blocklist.de (+dataplane.org,greensnow.co)""],

["121.33.237.102", "known attacker", ""blocklist.de (+dataplane.org,greensnow.co,rutgers.edu)""]]

表1

域名

121.241.244.92

known attacker

"blocklist.de (+dataplane.org,greensnow.co,rulez.sk,rutgers.edu)"

121.32.150.82

known attacker

"blocklist.de (+dataplane.org,greensnow.co)"

121.33.237.102

known attacker

"blocklist.de (+dataplane.org,greensnow.co,rutgers.edu)"

1.2 文件的写入

 文件的写入就相对简单很多,只需记得"a+"是追加写入文件、"W"是覆盖写入即可,直接贴出下面的代码:

with open("test.csv", "a+") as csvfile:

writer = csv.writer(csvfile)

writer.writerow(rows)

2. 在工程中的运用

 情景:面对一份上百万的trails.csv文件,如下图所示,现在我需要把第一列的中只含有ip或者是IP:port的那一行读取出来,并且放入新的文件中。

在这就需要使用正则表达式对ip进行匹配,匹配成功输入文件中。匹配IPV4及端口号的正则 Ad+.[0-9.]+|Ad+.[0-9.]+:[0-8]+。这里使用到search语句进行匹配,代码如下:

with open("trails.csv", "r", encoding = "utf-8") as f:

reader = csv.reader(f)

for rows in reader:

m = re.search(r"Ad+.[0-9.]+|Ad+.[0-9.]+:[0-8]+", rows[0])

if m:

with open("test.csv", "a+") as csvfile:

writer = csv.writer(csvfile)

writer.writerow(rows)

参考文献

[1]风景不在对岸wj.Python 读取csv的某行[EB/OL].https://blog.csdn.net/u011089523/article/details/52372962,2016-08-30.

[2]test_soy.Python 读取csv的某列[EB/OL].https://blog.csdn.net/test_soy/article/details/79718458?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control,2018-03-27.

以上是 python3处理csv文件 的全部内容, 来源链接: utcz.com/z/538051.html

回到顶部