简体   繁体   中英

How to I modify my code so I can search for multiple inputs(from the same column) at the same time in Python

So it is involves a previous question I posted, I gotten a lot good answers. But for this scenario, I want to enter more than one input at the same at the prompt, and search through a list of csv files.

For example:

Enter your strings:

11234567, 1234568. 1234569 etc.

(I want to set the parameter to be from 1 to 20)

And as for files input, is there a way to search for entire folder with files extensions with CSV, instead of hardcoding the names of csv files inside my code? So this way I don't have to keep adding names of CSV files in my code let's say if want to search through like 50 files. Is there a script like feature in Python to do this?

FYI, each input value I enter is distinct, so it cannot exist in 2 or more csv files at the same time.

Code:

import csv

files_to_open = ["C:/Users/CSV/salesreport1.csv","C:/Users/CSV//salesreport2.csv"]
data=[]

##iterate through list of files and add body to list
for file in files_to_open:
    csvfile = open(file,"r")
    reader = csv.reader(csvfile)
    for row in reader:
        data.append(row)
keys_dict = {}

column = int(input("Enter the column you want to search: "))
val = input("Enter the value you want to search for in this column: ")
for row in data:
    
    v = row[column]
    ##adds the row to the list with the right key
    keys_dict[v] = row
if val in keys_dict:
    print(keys_dict[val])
else:
    print("Nothing was found at this column and key!")
    

Also one last thing, how do I show the name of the csv file as a result too?

Enter the column to search: 0
Enter values to search (separated by comma): 107561898, 107607997
['107561898', ......]

Process finished with exit code 0

107561898 is the from column 0 from file 1, and 107607997 is the second value of that is stored in file 2(column 0)

as you can see, the result is only returning file that contains first input, where I want both input to be returned, so two record column 0 is where all the input values(card numbers are)

Seeing as you want to check a large number of files, here's an example of a very simple approach that checks all the CSVs in the same folder as this script.

This script allows you to specify the column and multiple values to search for.
Sample input:

Enter the column to search: 2
Enter values to search (separated by comma): leet,557,hello

This will search in the 3rd column for the worlds "leet", "hello" and the number 557.
Note that columns start counting at 0 , and there should be no extra spaces unless the keyword itself has a space char.

import csv
import os

# this gets all the filenames of files that end with .csv in the specified directory
# this is where the file finding happens, to answer your comment #1
path_to_dir = 'C:\\Users\\Public\\Desktop\\test\\CSV'
csv_files = [x for x in os.listdir(path_to_dir) if x.endswith('.csv')]

# get row number and cast it to an integer type
# will throw error if you don't type a number
column_number = int(input("Enter the column to search: "))

# get values list
values = input("Enter values to search (separated by comma): ").split(',')

# loops over every filename
for filename in csv_files:
    # opens the file
    with open(path_to_dir + '\\' + filename) as file:
        reader = csv.reader(file)
        # loops for every row in the file
        for row in reader:
            # checks if the entry at this row, in the specified column is one of the values we want
            if row[column_number] in values:
                # prints the row
                print(f'{filename}: {row}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM