Python专有文件转换循环

时间:2015-11-30 19:37:01

标签: python vlookup text-manipulation

目前正致力于专有文件格式的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"

我需要一个循环,它可以逐行进行并从原始文件中的一列复制数据,然后将其粘贴到一个新文件中,该文件将数据保存在不同的列中。有点像没有列标题的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

2 个答案:

答案 0 :(得分:1)

如上所述,csv模块可以处理引用的逗号。创建一个csv阅读器和一个编写器,然后唯一的技巧是过滤列。

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())

答案 1 :(得分:0)

如果文字不包含引号,您可以执行以下操作:

struct artist *update_counts(struct artist *a, struct play *p)
{
    struct artist *tmp_head = a;//tmp is the head of inputed linked list a 
    int count = 0;
    while (a != NULL)
    {       
        while (a->artist_id == p->artist_id)
        {
            count += p->playcount;
            p = p->next;
        }
        a->playcount = count;   
        a = a->next;
        count = 0;
    }
    return tmp_head;//just return the head of a 
}