'How to search for a keyword in multiple csv files

How can I use read_csv() functions to search for a keyword ex: EngineFuel" in multiple .csvif a folderoutput``` and return the file name if a match is found. The keyword is always in the first row of a csv file.



Solution 1:[1]

you can use regex to find for better performance

import pandas as pd
import re
def is_name_in_csv(word,csv_file):
  with open(csv_file,"r") as f:
    data = f.read()
  return bool(re.search(word, data))

list_file_csv = ['a.csv','b.csv','c.csv']
word = "EngineFuel"
for csv_file in list_file_csv:
  if is_name_in_csv(word,csv_file):
    print(f"find the {word} in {csv_file}")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tomerar