【问题标题】:Find and write to next blank cell in a column查找并写入列中的下一个空白单元格
【发布时间】:2018-10-10 20:30:12
【问题描述】:

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

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]

【问题讨论】:

  • 由于格式的原因,很难说出您到底在问什么。能不能稍微清理一下,让你的输入输出更清晰?
  • @Chris cmets 删除所有格式。您可以改为编辑您的问题。
  • 您提到的(相当奇怪的)输入是原始 csv 文件的产物吗?还是 ~* 分离的数据是您必须使用的原始数据?对于前者,您可以使用例如pandas 以更有效的方式解析您的输入。

标签: python python-3.x csv


【解决方案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

【讨论】:

    【解决方案2】:

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

    然后我会提供 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      相关资源
      最近更新 更多