Skip to content
Advertisement

What is wrong my python code, I’m trying to add implement multiple file search criteria for files

I want to make changes in my code so I can search for multiple input files and type multiple inputs, let’s say if client order number 7896547 exist in the input file I put there. What is the best way to implement multiple search criteria for multiple files.

What I meant is giving around let’s say like more than 50 inputs, 1234567, 1234568 etc……, and also search through multiple files(I mean more than 10). What is the most efficient way to achieve this?

My code:

JavaScript

I thought I can just add input by adding one file name in the open() part?

Also to add multiple input when typing, is there way to not use array?

I mean identifierlist = [“x”,”y”,”z”], not doing this

Advertisement

Answer

Just to be clear, you are loading in some .csvs, getting a name from the user, and then printing out the rows that have that name in column 0? This seems like a nice use case for a Python dictionary, a common and simple data type in Python. If you aren’t familiar, a dictionary lets you store information by some key. For example, you might have a key ‘Daniel’ that stores a list of information about someone named Daniel. In this situation, you could go through the array and put everyone row into a dict with the name as the key. This would look like:

JavaScript

and then to look up a name in the dict, you would just do:

JavaScript

or more generally,

JavaScript

You could also use the dict for getting your list of names and checking if a name exists, because in Python dicts have a built in method for finding if a key exists in a dict, “in”. You could just say:

JavaScript

or again,

JavaScript

Another cool thing you could do with this project would be to let the user choose what column they are searching. For example, let them put in 3 for the column, and then search on whatever is in column 3 of that row, location, email, etc.

Finally, I will just show a complete concrete example of what I would do:

JavaScript

EDIT: here’s a way to write a large number of text files and combine them into one file. For the multiple inputs, you could ask them to type commas, like “Daniel,Sam,Mike”… and then split the output on these commas with output.split(","). You could then do:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement