Emanuel Leago
Emanuel Leago

Reputation: 1

Search a CSV list file in a text file in python

How can I search a single raw CSV file (contains list of a lot of words) in a text file in python. Or simply I just want to find a list of words and want to know if they are exist in my text file or not. How can I do that in python? Or if I can do that with a specific application that made for something like this let me know, I want to try that in excel but it can't import CSV or list of words in advance search. Thank you in advance

Upvotes: 0

Views: 120

Answers (1)

Dimitrios Tsoukalas
Dimitrios Tsoukalas

Reputation: 11

  1. This program take as input a list of words with space between them and checks if they are in the CSV file.
  2. If they, the program add them to a list and print them.

Don't forget to change the name of your CSV file.

#!/usr/bin/python
import csv
from queue import Empty
import sys

#input the list of words you want to search and split words by SPACE
input_string = input('Enter elements of a list separated by space ')
string_list  = input_string.split()

#read csv, and split on "," the line
csv_file = csv.reader(open('convertcsv.csv', "r"), delimiter=",")

total = set()

#loop through the csv list
for row in csv_file:
    for field in row:
        total.add(field)

list=[]

#add the matches to a list
for input in string_list:
    if input in total:  
        if input not in list:
            list+=[input]


#print the list
print(list)

Upvotes: 1

Related Questions