查找并写入列中的下一个空白单元格

时间:2018-04-30 17:48:33

标签: python python-3.x csv

我需要查找并写入下一个空白单元格。

enter image description here

import csv

with open(r'C:\\filepath\file.txt', 'r')  as input_file:
    reader = csv.reader(input_file)

    with open (r'C:\filepath\file.csv', 'a', newline = '') as output_file:
        writer = csv.writer(output_file)
        for row in reader:
            content = [i.split('~') for i in row]
            for row1 in content:
                con = [len(k.split('*')) for k in row1]
                conn = [m.split('*') for m in row1]
                for b in conn:
                    if con[0] > 4:
                        if (b[0] == 'NM1' and b[1] == '82' and b[2] == '1' ):
                            writer.writerow([b[3]] + [b[4]])
                            print ( b[3] + b[4] )
                        elif (b[0] == 'GS' ):
                            writer.writerow(['','','',b[2]])
                            print(b[2])

寻求获得如上图所示的输出。现在在第一行只有'App1'打印,然后在第二行打印名称等输入文件我正在使用如下。 :

ISA*16* 00 0*T*>~ 
GS*IN*APP1*0999~ 
HPT*1*2~ SE*21*0001~ 
GE*1*145~
NM1*82*1*Tiger1a*Test1*K****~ 
NM1*82*1*Lion1a*Test2*K****~ 
NM1*82*1*Elephant1a*Test3*K****~ 
ISA*16* 00 0*T*>~ 
GS*IN*APP2*0999~ 
HPT*1*2~ SE*21*0001~ 
GE*1*145~ 
NM1*82*1*Tiger1a*Test4*K****~
ISA*16* 00 0*T*>~ 
GS*IN*APP1*0999~ 
HPT*1*2~ 
SE*21*0001~ 
GE*1*145~ 
NM1*82*1*Tiger1a*Test4*K****~ 
NM1*82*1*Lion1a*Test5*K****~ 
NM1*82*1*Elephant1a*Test6*K****~ 
ISA*16* 00 0*T*>~ 
GS*IN*APP10999~ 
HPT*1*2~ 
SE*21*0001~ 
GE*1*145~ 
NM1*82*1*Tiger1a*Test7*K****~ 

[![在此处输入图像说明] [2]] [2]

2 个答案:

答案 0 :(得分:0)

好的,我假设您有一个输入文件,其中'~'是记录分隔符,'*'是字段分隔符。由于csv模块只处理,我首先使用生成器将输入文件拆分为~

然后我会提供2个列表,其中一个列表以NM1*82*1开头,包含以下2个字段的列表,其中一个记录以GS开头,包含一个字段。

最后,我将第二个列表的每一行添加到第一个列表中的相应行。

代码可以是:

def splitter(fd, sep):
"""Splits fd (assumed to be an input file object) on sep ignoring end of lines"""
    last = ""
    for line in fd:
        lines = line.strip().split(sep)
        lines[0] = last + lines[0]
        last = lines.pop()
        for l in lines:
            yield(l.strip())
    if last != "":
        yield last.strip()
    return

with open(r'C:\\filepath\file.txt', 'r')  as input_file, \
        open (r'C:\filepath\file.csv', 'a', newline = '') as output_file:
    rd = csv.reader(splitter(input_file, '~'), delimiter='*')
    wr = csv.writer(output_file)
    ls1 = []
    ls2 = []
    for b in rd:
        if b[0] == 'NM1' and b[1] == '82' and b[2] == '1':
            ls1.append([b[3], b[4]])
        elif b[0] == 'GS':
            ls2.append(b[2])
    for i, b in enumerate(ls2):
        ls1[i].append(b)
    wr.writerows(ls1)

我获得:

Tiger1a,Test1,APP1
Lion1a,Test2,APP2
Elephant1a,Test3,APP1
Tiger1a,Test4,APP10999
Tiger1a,Test4
Lion1a,Test5
Elephant1a,Test6
Tiger1a,Test7

答案 1 :(得分:-1)

尝试使用行号作为键将文件读入单独的字典中。然后,您可以使用zip函数同时遍历这两个词典。

def zip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    sentinel = object()
    iterators = [iter(it) for it in iterables]
    while iterators:
        result = []
        for it in iterators:
            elem = next(it, sentinel)
            if elem is sentinel:
                return
            result.append(elem)
        yield tuple(result)

此处有更多信息:Python3 zip function