×

Good News Everybody!

The new search engine is LIVE!

Please report any problems to david (at) midrange.com.




Thanks Kevin. These little examples make great building blocks for me to work from.

A couple of months ago I was able to whip up a little Python program to retrieve data we need on a thrice hourly retrieval from a web service. So very simple.

Now, I have snippets for XLS, CSV and the DB2 connect.

Keep 'em coming...


-----Original Message-----
From: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Kevin Adler
Sent: Wednesday, November 16, 2016 3:32 PM
To: Midrange Systems Technical Discussion
Subject: Re: Quoting text strings for SCV output

Yep. Using the CSV module is pretty easy, here's a similar example:

import ibm_db_dbi as db2
import csv

def trim_col(s):
try:
return s.rstrip()
except AttributeError:
return s

conn = db2.connect()
cursor = conn.cursor()

query = "select cusnum, lstnam, init, cdtlmt from qiws.qcustcdt where
cdtlmt > 100"
filename = 'qcustcdt.csv'

with open(filename, 'w', newline='') as csvfile:
cursor.execute(query)

writer = csv.writer(csvfile, dialect='excel')

for row in cursor:
writer.writerow([trim_col(col) for col in row])


Note this doesn't include headers. If you want headers, you can pass an
array of headers using the fieldnames parameter to the csv.writer function
and then call writer.writeheaders():

with open(filename, 'w', newline='') as csvfile:
cursor.execute(query)

writer = csv.writer(csvfile, dialect='excel', fieldnames=[descr[0] for
descr in cur.description])
writer.writeheaders()

for row in cursor:
writer.writerow([trim_col(col) for col in row])

See https://docs.python.org/3.4/library/csv.html for more information on
how to further customize the output.

"MIDRANGE-L" <midrange-l-bounces@xxxxxxxxxxxx> wrote on 11/16/2016
05:16:37 PM:

From: John Yeung <gallium.arsenide@xxxxxxxxx>
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxx>
Date: 11/16/2016 05:17 PM
Subject: Re: Quoting text strings for SCV output
Sent by: "MIDRANGE-L" <midrange-l-bounces@xxxxxxxxxxxx>

On Wed, Nov 16, 2016 at 5:38 PM, Roger Harman
<roger.harman@xxxxxxxxxxx> wrote:
Wouldn't the csv module be a workable solution?

Yes, which is why I injected Python into the thread. The CSV module in
Python's standard library can read or write either Excel-dialect CSV
or OP's mandatory-double-quoted-string dialect.

To minimize reworking of OP's existing tools (which generate .xls or
.xlsx) and eliminate the Excel program itself (as a .xls(x)-to-.csv
translator), Python could be used to read the workbook directly (and
quite trivially), using the xlrd third-party module.

John Y.
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/midrange-l.

Please contact support@xxxxxxxxxxxx for any subscription related
questions.




As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2026 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.