× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I honestly do not mean to spam the lists with Python recommendations,
but this is really a perfect place to use Python. It has a built-in
CSV module so the parsing is robust and dead-simple; and if you use
iSeriesPython, you can write directly to a PF with either RLA or SQL.

Using the stream file APIs from RPG is certainly fine, but... well, it
involves more boilerplate and more fumbling around if you haven't
already built a CSV-parsing service program.

Using iSeriesPython for this is stupidly easy. Unfairly easy. Here, I
will give you the code:

### begin script

import csv
from operator import itemgetter

import db2 # this is specific to iSeriesPython; for other Pythons,
use ibm_db_dbi instead

# Pick the 14 columns by ordinal number, using 0 for the first column.
selected_columns = itemgetter(0, 1, 2, 5, 7, 9, 11, 12, 51, 104, 105,
108, 120, 134)
placeholders = ', '.join('?' * 14)

connection = db2.connect(autocommit=True)
c1 = connection.cursor()

# Get the data types of the columns.
c1 = execute('select * from mylib.mytable')
types = [str(t[1]) for t in c1.description]

with open('mydata.csv') as f:
reader = csv.reader(f)
next(reader) # consume header; if no header, remove this line
for row in reader:
elements = []
for val, t in zip(selected_columns(row), types):
elements.append(val if t == 'STRING' else float(val))
c1.execute('''
insert into mylib.mytable values ({})
'''.format(placeholders), elements)

### end script

If you know ahead of time that all the columns you want are string, or
all numeric, then you can simplify the code.

If you use a Python other than iSeriesPython, then there are other
(relatively minor) adjustments that need to be made.

I'm happy to help with explanations or code adjustments.

John Y.

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-2024 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.