title: How to search for a 'text' or 'number' in a csv file with Python AND if exists print only first and second column values to a new csv file
body: <p>I want to do the following using Python.</p>
  
  <pre><code>Step-1: Read a specific third column on a csv file using Python.
  Step-2: Create a list with values got from step-1
  Step-3: Take the value of index[0], search in csv file, if present print the values of column 1 and 2 only to a new csv file(There are 6 columns). If Not presents just ignore and goto next search.
  </code></pre>
  
  <p><strong>file1.csv:</strong></p>
  
  <pre><code>Country,Location,number,letter,name,pup-name,null
  a,ab,1,qw,abcd,test1,3
  b,cd,1,df,efgh,test2,4
  c,ef,2,er,fgh,test3,5
  d,gh,3,sd,sds,test4,
  e,ij,5,we,sdrt,test5,
  f,kl,6,sc,asdf,test6,
  g,mn,7,df,xcxc,test7,
  h,op,8,gb,eretet,test8,
  i,qr,8,df,hjjh,test9,
  </code></pre>
  
  <p><strong>Python script written for this:</strong></p>
  
  <pre><code>import csv
  import time
  from collections import defaultdict
  
  columns = defaultdict(list)
  
  with open('file1.csv') as f:
      reader = csv.reader(f)
      reader.next()
      for row in reader:
          for (i,v) in enumerate(row):
              columns[i].append(v)
  #print(columns[2])
  
  b=(columns[2])
  for x in b[:]:
      time.sleep(1)
      print x
  </code></pre>
  
  <p><strong>Output of above script:</strong></p>
  
  <pre><code>MacBook-Pro:test_usr$ python csv_file.py 
  1
  1
  2
  3
  5
  6
  7
  8
  8
  MacBook-Pro:test_usr$
  </code></pre>
  
  <p>I am able to do the steps 1 and 2.</p>
  
  <p>Please guide me on doing Step-3. That is how to search for text/string in csv file and if present how to extract only specific column values to a new csv file?</p>
  
  <p><strong>Output file should look like:</strong></p>
  
  <pre><code>a,ab
  b,cd
  c,ef
  d,gh
  e,ij
  f,kl
  g,mn
  h,op
  i,qr
  </code></pre>
  
  <p>Note : Search string will be from another csv file. Please don't suggest the direct answer for printing values of column 1 and 2 directly. </p>
  
  <p><strong>FINAL CODE is looks this:</strong></p>
  
  <pre><code>import csv
  import time
  from collections import defaultdict
  
  columns = defaultdict(list)
  
  with open('file1.csv') as f:
      reader = csv.reader(f)
      reader.next()
      for row in reader:
          for (i,v) in enumerate(row):
              columns[i].append(v)
              b=(columns[2])
  
              for x in b[:]:
                  with open('file2.csv') as f, open('file3.csv', 'a') as g:
                      reader = csv.reader(f)
                      #next(reader, None) # discard the header
                      writer = csv.writer(g)
                      for row in reader:
                          if row[2] == x:
                              writer.writerow(row[:2])
  </code></pre>
  
  <p>file1.csv:</p>
  
  <pre><code>Country,Location,number,letter,name,pup-name,null
  a,ab,1,qw,abcd,test1,3
  b,cd,1,df,efgh,test2,4
  c,ef,2,er,fgh,test3,5
  d,gh,3,sd,sds,test4,
  e,ij,5,we,sdrt,test5,
  f,kl,6,sc,asdf,test6,
  g,mn,7,df,xcxc,test7,
  h,op,8,gb,eretet,test8,
  i,qr,8,df,hjjh,test9,
  </code></pre>
  
  <p>file2.csv:</p>
  
  <pre><code>count,name,number,Type,status,Config Version,,IP1,port
  1,bob,1,TRAFFIC,end,1.2,,1.1.1.1,1
  2,john,1,TRAFFIC,end,2.1,,1.1.1.2,2
  4,foo,2,TRAFFIC,end,1.1,,1.1.1.3,3
  5.333333333,test,3,TRAFFIC,end,3.1,,1.1.1.4,4
  6.833333333,raa,5,TRAFFIC,end,5.1,,1.1.1.5,5
  8.333333333,kaa,6,TRAFFIC,end,7.1,,1.1.1.6,6
  9.833333333,thaa,7,TRAFFIC,end,9.1,,1.1.1.7,7
  11.33333333,paa,8,TRAFFIC,end,11.1,,1.1.1.8,8
  12.83333333,maa,8,TRAFFIC,end,13.1,,1.1.1.9,9
  </code></pre>
  
  <p>If I run the above script, output of file3.csv:</p>
  
  <pre><code>1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  1,bob
  2,john
  .
  .
  .
  
  Its goes like this in loop
  </code></pre>
  
  <p>But output should be like this:</p>
  
  <pre><code>count,name
  1,bob,
  2,john,
  4,foo,
  5.333333333,test,
  6.833333333,raa,
  8.333333333,kaa,
  9.833333333,thaa,
  11.33333333,paa,
  12.83333333,maa,
  </code></pre>