title: python csv, writing headers only once
body: <p>So I have a program that creates CSV from .Json.</p>
  
  <p>First I load the json file.</p>
  
  <pre><code>f = open('Data.json')
  data = json.load(f)
  f.close()
  </code></pre>
  
  <p>Then I go through it, looking for a specific keyword, if I find that keyword. I'll write everything related to that in a .csv file.</p>
  
  <pre><code>for item in data:
      if "light" in item:
         write_light_csv('light.csv', item)
  </code></pre>
  
  <p>This is my <code>write_light_csv</code> function :</p>
  
  <pre><code>def write_light_csv(filename,dic):
  
      with open (filename,'a') as csvfile:
          headers = ['TimeStamp', 'light','Proximity']
          writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)
  
          writer.writeheader()
  
          writer.writerow({'TimeStamp': dic['ts'], 'light' : dic['light'],'Proximity' : dic['prox']})
  </code></pre>
  
  <p>I initially had <code>wb+</code> as the mode, but that cleared everything each time the file was opened for writing. I replaced that with <code>a</code> and now every time it writes, it adds a header. How do I make sure that header is only written once?.</p>