Python OrderedDict to CSV: Eliminating Blank Lines
<p>When I run this code...</p>

<pre><code>from simple_salesforce import Salesforce
sf = Salesforce(username='un', password='pw', security_token='tk')
cons = sf.query_all("SELECT Id, Name FROM Contact WHERE IsDeleted=false LIMIT 2")

import csv

with open('c:\test.csv', 'w') as csvfile:
    fieldnames = ['contact_name__c', 'recordtypeid']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for con in cons['records']:    
        writer.writerow({'contact_name__c': con['Id'], 'recordtypeid': '082I8294817IWfiIWX'})

print('done')
</code></pre>

<p>I get the following output inside my CSV file...</p>

<pre><code>contact_name__c,recordtypeid

xyzzyID1xyzzy,082I8294817IWfiIWX

abccbID2abccb,082I8294817IWfiIWX
</code></pre>

<p>I'm not sure why those extra lines are there.</p>

<p><strong>Any tips for getting rid of them so my CSV file will be normal-looking?</strong></p>

<p>I'm on Python 3.4.3 according to <code>sys.version_info</code>.</p>

<hr>

<p>Here are a few more code-and-output pairs, to show the kind of data I'm working with:</p>

<pre><code>from simple_salesforce import Salesforce
sf = Salesforce(username='un', password='pw', security_token='tk')
print(sf.query_all("SELECT Id, Name FROM Contact WHERE IsDeleted=false LIMIT 2"))
</code></pre>

<p>produces</p>

<pre><code>OrderedDict([('totalSize', 2), ('done', True), ('records', [OrderedDict([('attributes', OrderedDict([('type', 'Contact'), ('url', '/services/data/v29.0/sobjects/Contact/xyzzyID1xyzzy')])), ('Id', 'xyzzyID1xyzzy'), ('Name', 'Person One')]), OrderedDict([('attributes', OrderedDict([('type', 'Contact'), ('url', '/services/data/v29.0/sobjects/Contact/abccbID2abccb')])), ('Id', 'abccbID2abccb'), ('Name', 'Person Two')])])])
</code></pre>

<p>and</p>

<pre><code>from simple_salesforce import Salesforce
sf = Salesforce(username='un', password='pw', security_token='tk')
cons = sf.query_all("SELECT Id, Name FROM Contact WHERE IsDeleted=false LIMIT 2")
for con in cons['records']:
    print(con['Id'])
</code></pre>

<p>produces</p>

<pre><code>xyzzyID1xyzzy
abccbID2abccb
</code></pre>