Search code examples
pythoncsvline-breaksordereddictionary

Python OrderedDict to CSV: Eliminating Blank Lines


When I run this 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')

I get the following output inside my CSV file...

contact_name__c,recordtypeid

xyzzyID1xyzzy,082I8294817IWfiIWX

abccbID2abccb,082I8294817IWfiIWX

I'm not sure why those extra lines are there.

Any tips for getting rid of them so my CSV file will be normal-looking?

I'm on Python 3.4.3 according to sys.version_info.


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

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"))

produces

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')])])])

and

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'])

produces

xyzzyID1xyzzy
abccbID2abccb

Solution

  • Two likely possibilities: the output file needs to be opened in binary mode and/or the writer needs to be told not to use DOS style line endings.

    To open the file in binary mode in Python 3 replace your current with open line with:

    with open('c:\test.csv', 'w', newline='') as csvfile:
    

    to eliminate the DOS style line endings try:

    writer = csv.DictWriter(csvfile, fieldnames=fieldnames, lineterminator="\n")