我有一个非常大的 .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/