Python 专有文件转换循环

Python proprietary file conversion loop

目前正在为专有文件格式进行 EDI 翻译。输入文件可能有成百上千条记录。每一行都是一条记录。

INFILE:(忽略开头的空行,它只是为了视觉表示)

"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"

我需要一个循环,它可以逐行复制原始文件中一列的数据,并将其粘贴到一个新文件中,该文件将数据保存在不同的列中。有点像没有列 headers 的 vlookup。这是列号之间的关系。

InColumn-OutColumn 
1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 12-9, 14-10, 25-11, 68-24

提前致谢,我似乎无法全神贯注。

编辑:这里要求的是我无法开始工作的损坏代码。

KEY = [1,2,3,4,5,6,7,8,12,14,25,68]
Body850 = open(TEMP, 'r+')

for line in Body850:
    for x in line.split(','):
        END += KEY[x]
    print line

如果文本不包含引号,您可以这样做:

for line in Body850:
    for x in line.split('","'):
        if x in KEY:
             END += KEY[x]
        else
             pass # do something here in case x is not in KEY
    print line

如前所述,csv 模块可以处理引号中的逗号。创建一个 csv reader 和一个 writer,然后唯一的技巧就是过滤列。

import csv

# todo: for testing...
open('testfile.csv', 'w').write('''"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"
"1","2","3","4","5","6","7","can also contain text, and commas","9","10","11","12","13"''')

# the columns wanted, in order they should appear in output (zero based)
# ...example doesn't go to 68 cols, so abreviated
column_order = (0,1,2,3,4,5,6,7,11)

with open('testfile.csv') as csvin:
    reader = csv.reader(csvin)
    with open('testfile_new.csv', 'w') as csvout:
        writer = csv.writer(csvout)
        for row in reader:
            writer.writerow([row[i] for i in column_order])

# todo: for testing...
print(open('testfile_new.csv').read())