Python Forum
Search Excel File with a list of values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search Excel File with a list of values
#2
I would read the excel spreadsheet and extract a list of department, machine ID strings. I would format the strings to match the format of the department, machine ID in the csv files. Something like this:
df = pd.read_excel("machines.xlsx")
machine_names = [
    f"{dpt}, {id}" for dpt, id in zip(df["Department"].values, df["Machine"].values)
]
print(machine_names)
I assumed the spreasheet filename is "machines.xlsx", that it has column headers, and that the column headers for the department and machine ID columns are "Department" and "Machine". If the spreadsheet doesn't have column headers you will use index values 0, 1, 2 for spreadsheet columns A, B, C, like this:
df = pd.read_excel("machines.xlsx", header=None)
machine_names = [f"{dpt}, {id}" for dpt, id in zip(df[0].values, df[1].values)]
Now you can use this list to check for matches in the CSV files. Something like this:
csv_folder = '.'
for filename in Path(csv_folder).glob("*.csv"):
    with open(filename, "r") as file:
        values = file.readline().split(",")
    if len(values) > 1:
        machine_name = f"{values[0].strip()}, {values[1].strip()}"
        if machine_name in machine_names:
            print(machine_name)   # Match found.  Do your processing here
This code assumes all the csv files will be in some folder ("." is the current working directory) and that the files all have the extensions ".csv". It opens the file, splits the first line into columns using a comma as the delimiter, and makes a string using the department name and machine ID. This format must match the format of the strings constructed from the excel spreadsheet.

To determine if the CSV file is a valid machine, test if the csv machine string matches any of the machine strings from the spreadsheet.
Reply


Messages In This Thread
Search Excel File with a list of values - by huzzug - Oct-25-2023, 06:14 PM
RE: Search Excel File with a list of values - by deanhystad - Oct-27-2023, 06:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 406 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 510 Feb-07-2024, 12:24 PM
Last Post: Viento
  Copying the order of another list with identical values gohanhango 7 1,241 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Updating sharepoint excel file odd results cubangt 1 935 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  trouble reading string/module from excel as a list popular_dog 0 461 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,471 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  Search Outlook Inbox for set of values cubangt 1 1,197 Jun-28-2023, 09:29 PM
Last Post: cubangt
  Comparing List values to get indexes Edward_ 7 1,272 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,168 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  search file by regex SamLiu 1 961 Feb-23-2023, 01:19 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020