简体   繁体   中英

Finding the matching strings in a csv against a list of string?

I am trying to figure out how to search for a specific string and print the lines where the string is found from a csv.

I have a CSV with data in the format like this

jeffrey,192.168.1.1,example1.com,30220,internet serverice provider 1
mike,192.168.1.2,example2.com,30220,internet service provider 1
frank,192.168.1.3,example3.com,30220,internet service provider 1
lucy,192.168.1.4,example4.com,14619,internet service provider 2
louisa,192.168.1.5,example5.com,14619,internet service provider 2
emily,192.168.1.6,example6.com,3357,internet service provider 3 
john,192.168.1.7,example7.com,210,internet service provider 4

Lets say I have a list that contains the numbers I want to find which are "14619" and "210". I want to write in python a way that can print out the lines that the string are found. So that the result I want is

lucy,192.168.1.4,example4.com,14619,internet service provider 2
louisa,192.168.1.5,example5.com,14619,internet service provider 2
john,192.168.1.7,example7.com,210,internet service provider 4

I am assuming you want to use a if statement for something like this

Here is my python code, I only got the csv to open and print itself.

import csv

document= open("") #filelocation 
with document as file:
    reader = csv.reader(file)
    count = 0 
    for row in reader:
        print(row)

Any help is greatly appreciated.

nums = ["14619", "210"]
for row in reader:
    if row[3] in nums:
        print(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM