Search for a specific value in a specific column with Python
<p>I've got a text file that is tab delimited and I'm trying to figure out how to search for a value in a specific column in this file.</p>

<p>I think i need to use the csv import but have been unsuccessful so far. Can someone point me in the right direction?</p>

<p>Thanks!</p>

<p><em><strong></em>**Update**</strong>
Thanks for everyone's updates. I know I could probably use awk for this but simply for practice, I am trying to finish it in python. </p>

<p>I am getting the following error now:
if row.split(' ')[int(searchcolumn)] == searchquery:
IndexError: list index out of range</p>

<p>And here is the snippet of my code:</p>

<pre><code>#open the directory and find all the files
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        f=open(file, 'r')
        lines=f.readlines()
        for line in lines:
            #the first 4 lines of the file are crap, skip them
            if linescounter &gt; startfromline:
                with open(file) as infile:
                    for row in infile:
                        if row.split(' ')[int(searchcolumn)] == searchquery:
                            rfile = open(resultsfile, 'a')
                            rfile.writelines(line) 
                            rfile.write("\r\n")
                            print "Writing line -&gt; " + line
                            resultscounter += 1
        linescounter += 1
        f.close()
</code></pre>

<p>I am taking both searchcolumn and searchquery as raw_input from the user. Im guessing the reason I am getting the list out of range now, is because it's not parsing the file correctly? </p>

<p>Thanks again.</p>
