0

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
k..
  • 401
  • 3
  • 11
  • 2
    Possible duplicate of [CSV file written with Python has blank lines between each row](http://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row) – Moses Koledoye Jul 20 '16 at 19:14
  • Thanks, I hadn't found that one - probably because I kept putting "ordereddict" into my search. – k.. Jul 20 '16 at 20:06

1 Answers1

1

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")
Feneric
  • 853
  • 1
  • 11
  • 15
  • Thanks - it works with either of your suggestions, as well as with both of them together. Observations: When I open them in Helios Software Solutions's "Textpad," the 1-or-the-other output files have 2 spaces at the end of each line in the highlighting when I "ctrl+a," but no extra end-padding when I, say, cursor through the text or click beyond the end of the line. The "both-and" output has just 1 space of "ctrl+a" highlighting overshoot in this software and, again, no "perceptible" spaces otherwise. All 3 open the same to the naked eye in Excel and, I'm sure, will work for my purpose. – k.. Jul 20 '16 at 19:34
  • 1
    You're welcome. Yeah, a lot depends on your environment. Different platforms have different assumptions about line endings, and different editors treat different types of line endings in different ways. DOS-style line endings use two characters rather than the one used by Linux / UNIX / Mac / BSD / etc. – Feneric Jul 20 '16 at 20:09