CSV Input and output into a searchable window


#1

I am just beginning to learn coding, but I have a specific goal in mind.

I have multiple csv files that I want to search and show specific results

for example in csv x there is a line of information and if I search for one of those things in the line then the entire line will appear.

I also want to add adjustable text boxes to the window and even pictures to the results.

if someone could help me Id appreciate it


#2

We can assume that the CSV is read in, and we’re able to retrieve line by line. The file structure is perfectly married to Python’s file tools. Each line read in would be stored to a list, as a tuple. Since CSV lines terminate with a newline, our code would need to strip the last character of the line before splitting it (slice the last character).

This is a generalized view, as there are many ways we could parse and implement the data, but it does give us a data structure that is modularized.

Two things we know about CSV that can help us along: 1. all data is in string format, and 2. all data corresponds to a perceivable column heading.

If we choose to objectify the data, it is a simple matter of reading each row, and assign from a list of pre-defined keys the data as it occurs positionally in the tuple.

>>> csv_data = "one", "two", "three", "four"
>>> keys = ['first', 'second', 'third', 'fourth']
>>> data_list = [csv_data]
>>> data = {keys[key]: value for row in data_list for key in range(len(keys)) for value in row}
>>> data
{'fourth': 'four', 'third': 'four', 'first': 'four', 'second': 'four'}
>>> 

Obviously we’ve missed something here, but I’ll get to bottom of it.


Well, d’uh. Three loops nested?

>>> csv_data = "one", "two", "three", "four"
>>> data_list = [csv_data]
>>> data = {keys[index]: row[index] for row in data_list for index in range(len(row))}
>>> data
{'fourth': 'four', 'third': 'three', 'first': 'one', 'second': 'two'}
>>> 

Now all we need to is append this dictionary to a new data structure, or replace that row of the data list.


That would assume we had the correct code working, which the above is not. Following is a refinement that one could expect will work on a larger data set.

>>> csv_line_1 = "one", "two", "three", "four"
>>> csv_line_2 = "une", "deux", "trois", "quatre"
>>> data_list = [csv_line_1, csv_line_2]
>>> data = [{keys[index]: row[index] for index in range(len(row))} for row in data_list]
>>> data
[{'fourth': 'four', 'third': 'three', 'first': 'one', 'second': 'two'}, {'fourth': 'quatre', 'third': 'trois', 'first': 'une', 'second': 'deus'}]
>>> 

As for searchability,

>>> search = "quatre"
>>> for index, content in enumerate(data):
	for key in content:
		if content[key] == search:
			print (data[index])
			break

		
{'fourth': 'quatre', 'third': 'trois', 'first': 'une', 'second': 'deux'}
>>> 

For a different perspective, …

>>> db = []
>>> db.append(("one", "two", "three", "four"))
>>> db.append(("une", "deux", "trois", "quatre"))
>>> data = [{keys[idx]: row[idx] for idx in range(len(row))} for row in db]
>>> for row in data:
	for key in row:
		print ("{}: {}".format(key, row[key]))
		
fourth: four
third: three
first: one
second: two
fourth: quatre
third: trois
first: une
second: deux
>>> search = "quatre"
>>> for index, content in enumerate(data):
	for key in content:
		if content[key] == search:
			print (data[index])
			break
		
{'fourth': 'quatre', 'third': 'trois', 'first': 'une', 'second': 'deux'}
>>> 

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.