python,循环管道分隔文本文件并运行SQL

时间:2015-08-06 00:37:58

标签: python unix

我正在尝试循环管道分隔的文本文件,并将每列的参数解析为sql文件: 我需要第1列和第3列来解析sql文件。

文字文件
557438 | | ZZCS0 | ZZ
557439 | | ZZJM | ZZ

fileHandle = open('/tmp/so_insert_report20150804.txt', 'r')
for line in fileHandle:
    fields = line.split('|')

    os.system("prosql -n /psd_apps/700p6/cus/so_insert.enq" fields[0] fields[2])

fileHandle.close()

1 个答案:

答案 0 :(得分:0)

最好使用上下文管理器打开文件:使用" with"。另外,我建议您使用subprocess.check_call函数代替os.system,如下所示:

from subprocess import check_call

with open('/tmp/so_insert_report20150804.txt') as fd:
    for line in fd:
        c1, c2, c3, c4 = line.strip().split('|')
        check_call(['prosql', '-n', '/psd_apps/700p6/cus/so_insert.enq', c1, c3])

检查子进程模块:

https://docs.python.org/3/library/subprocess.html

顺便说一句,因为我怀疑你是否计划修改数据库,最好以更交易的方式执行调用,即在执行调用之前验证令牌,如下所示:

from subprocess import check_call

def validatec1(c1):
    return str(int(c1))

def validatec3(c3):
    c3 = c3.strip()
    if not c3:
        raise Exception('Column 3 {} is empty'.format(c3))
    if not c3.startswith('ZZ'):
        raise Exception('Invalid value for c3 {}'.format(c3))
    return c3

batch = []

with open('/tmp/so_insert_report20150804.txt') as fd:
    for lnum, line in enumerate(fd, 1):
        try:
            c1, c2, c3, c4 = line.strip().split('|')
            batch.append((validatec1(c1), validatec3(c3)))
        except Exception as e:
            print('Error processing input file at line {}:\n{}'.format(lnum, line))
            raise e

for v1, v2 in batch:
    check_call(['prosql', '-n', '/psd_apps/700p6/cus/so_insert.enq', v1, v2])