0

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

2
  • Can you share some example of the input and the expected output? Your description is too vague. As you described it here you could solve it with pure python, using python's regex or even with a simple text editor with a find function Commented Jul 16, 2022 at 11:32
  • Input is some words include number and alphabet and output can be just a simple warning or popup to know if it's find that word or export in text file. Commented Jul 16, 2022 at 11:38

1 Answer 1

1
  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)
Sign up to request clarification or add additional context in comments.

6 Comments

This will be very slow if total is large, Make total a set, then you can do if value in total and avoid the inner loop. And stylistically, don't use Python keywords (input, list) as variable names.
You could also make string_list a set, and then take the intersection of it and total.
But just one thing I have about 20,000 words, I can't copy them in my code. is it possible to read them from my CSV file and find them in my text file?
My text file is more than 500,000 lines and my CSV list words are 20,000 that's too much information I cant insert them separately word by word in the code
and I want to run that in linux, if that's help for import or addressing my files
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.