Python:如何根据元组中指定的多个条件从 csv 文件中提取行

标签 python csv python-2.7

我有一个非常大的 .csv 文件 (10GB),想要根据元组中的不同条件提取行。
每行第四列包含 IPAdd
我只需要提取具有特定 IP 的行。

我是 python 新手,想知道如何迭代每个元组 IP 并将它们写入 WYE_Data.csv 文件。

CSV 文件的内容示例为;

xxx,1234,abc,199.199.1.1,1,fghy,xxx   
xxx,1234,abc,10.10.1.1,1,fghy,xxx   
xxx,1234,abc,144.122.1.1,1,fghy,xxx   
xxx,1234,abc,50.200.50.32,1,fghy,xxx

import csv   
customers = csv.reader(open('data.csv', 'rb'), delimiter=',')    
## This is the line I'm having         issues with   
IPAdd = ('199.199.1.1' or '144.122.1.1' or '22.22.36.22')
csvout = csv.writer(open('WYE_Data.csv', 'ab')) 

for customer in customers:   
    if customer[3] == IPAdd:    
        csvout.writerow(customer)

最佳答案

我建议您使用要匹配 IP 的值列表。

ips = ['199.199.1.1', '144.122.1.1', '22.22.36.22']

然后你可以说:

if customer[3] in ips:

关于Python:如何根据元组中指定的多个条件从 csv 文件中提取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10983553/

相关文章:

python - 使用 pandas 将年、月、日列合并为单个日期列

python - 从 python 2.7 中的列表中删除每个第 n 个元素

python - “引擎”对象没有属性 'drivername'

python - Pandas 和日期时间类型错误 : cannot compare a TimedeltaIndex with type float

python - 根据Python中另一个文件中的元素对文本文件进行排序

python - 如何在Python多处理中使进程休眠而不使其他进程休眠

c# - 创建csv文件时,Excel显示双引号

macos - 使用 Sed Mac 终端查找并替换为空格

python - 基于规则的 Spacy 实体匹配器

python - 从 bs4 的动态类 ="????"获取数据