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
-
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 functionRodrigo Laguna– Rodrigo Laguna2022-07-16 11:32:47 +00:00Commented 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.Emanuel Leago– Emanuel Leago2022-07-16 11:38:41 +00:00Commented Jul 16, 2022 at 11:38
Add a comment
|
1 Answer
- This program take as input a list of words with space between them and checks if they are in the CSV file.
- 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)
6 Comments
snakecharmerb
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.snakecharmerb
You could also make
string_list a set, and then take the intersection of it and total.Emanuel Leago
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?
Emanuel Leago
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
Emanuel Leago
and I want to run that in linux, if that's help for import or addressing my files
|