Creating a keyword based search in python
Question: I have a giant CSV file with close to 6K entries and the file looks something
like this:

    
    
    PDB ID  NDB ID  Structure Title  Citation Title     Abstract
    1ET4    1ET4    Structure of     Solution structure Research was performed and 
                    haemoglobin mRNA of mRNA aptamer    structure of mRNa was obtained
                    aptamer.
    

My end goal is to display an output given a keyword like so:

    
    
    Keyword: mRNA
    PDB ID   NDB ID   Structure Title   Citation Title   Abstract   Location of first hit
                                                                    struc/citation/abstract
    

What would be a starting point for me? Also, do I have to use something called
Regex for this?

Disclaimer: This is part of a research project, not school homework.

A pseudocode or template would be great for me.


Answer: You could parse the CSV file and create two data structures. Both
dictionaries.

One dictionary would contain each line, keyed on `PDB ID`. The other
dictionary would store sets of `PDB ID`s and be keyed on keywords.

Below is just example code because I'm ignoring headers. You probably want to
parse the csv properly...

    
    
    from collections import defaultdict
    entries = {}
    keywords = defaultdict(set)
    
    with open('my_csv.csv') as f:
        for line in f:
            entries[line.split()[0]] = line  # so keying on the PDB ID
    
    with open('my_csv.csv') as f:
        for line in f:
            for kw in line.split()[1:]
                keywords[kw].add(line.split()[0])
    

Once you have those two data structures it should be trivial to look up a
keyword in the keywords dict, iterate over the set, and print out each line
with the relevant PDB id.

