Python 使用逗号作为分隔符从文本文件中读取一行

Python 使用逗号作为分隔符从文本文件中读取一行,python,text,file-io,delimiter,Python,Text,File Io,Delimiter,我正在尝试从文本文件中读取不同的行 当前我有一个程序,可以从文本文件中读取以下类型的行,如果它遵循以下格式: 6361550850261,SHOWALL APN="3" IGF=15 VOW=117 VWD=12 [+][+]52 使用此代码: def make_dict(data): return dict((line.split(None, 1)[0], line)for line in data) def process(infile, outfile, keywords):

我正在尝试从文本文件中读取不同的行

当前我有一个程序,可以从文本文件中读取以下类型的行,如果它遵循以下格式:

6361550850261,SHOWALL
APN="3"
IGF=15
VOW=117
VWD=12
[+][+]52
使用此代码:

def make_dict(data):
    return dict((line.split(None, 1)[0], line)for line in data)

def process(infile, outfile, keywords):
    keys = [[k[0], k[1], 0] for k in keywords]
    endk = None
    with open(infile, 'rb') as fdin:
        with open(outfile, 'ab') as fdout:
            fdout.write("|<" + words + ">|" + "\r\n")
            for line in fdin:
                if endk is not None:
                    fdout.write(line)
                    if line.find(endk) >= 0:
                        fdout.write("\r\n")
                        endk = None
                else:
                    for k in keys:
                        index = line.find(k[0])
                        if index >= 0:
                            fdout.write(line[index + len(k[0]):].lstrip())
                            endk = k[1]
                            k[2] += 1
    if endk is not None:
        print 'Serial Number not Found'
        raise Exception(endk + "Not found before end of file")
    return keys
请注意,我使用逗号而不是空格作为分隔符来分隔数据

我如何使用与顶部文本文件相同的想法

一般来说,我只是想读一行使用这种格式而不是另一种格式的

编辑:

因此,作为一个输出示例: 如果我有这句话:
*GS,63615508502611635181215,APN;“3”,IGF:A;15;誓言:117,大众汽车;12,ADC:12.40;[+][+]52

为此:

*GS
6361550850261
211635181215
APN:“3”
胰岛素样生长因子:A;15
誓言:117
大众:12

ADC:12.40

按字符串中的逗号拆分字符串:

>>> s = '*GS,6361550850261,211635181215,,APN;"3",IGF:A;15;VOW:117,VWD;12,ADC:12.40;[+][+]52'
>>> lines = [line for line in s.split(',') if line]
>>> lines
['*GS', '6361550850261', '211635181215', 'APN;"3"', 'IGF:A;15;VOW:117', 'VWD;12', 'ADC:12.40;[+][+]52']
现在,您可以在这些行上迭代,以便根据需要对它们进行处理


请注意,此代码可能无法解决您的问题,因为示例的语法存在差异。请记下并重新调整我的答案以满足您的需要。

对于您给定的输入示例,以下函数应为您提供所需的结果:

import csv

def process(infile, outfile):
    with open(infile, 'rb') as f_input, open(outfile, 'w') as f_output:
        for cols in csv.reader(f_input):
            output = cols[0:3]                          # *GS + 2 numbers
            output.append(cols[4].replace(';', ':'))    # APN
            output.extend(cols[5].rsplit(';', 1))       # IGF and VOW
            output.append(cols[6].replace(';', ':'))    # VWD
            output.append(cols[7].rsplit(';', 1)[0])    # ADC
            f_output.write('\n'.join(output))
            f_output.write('\n')
这将生成一个输出文件,其中包含:

*GS
6361550850261
211635181215
APN:“3”
胰岛素样生长因子:A;15
誓言:117
大众:12
艺术发展局:12.40
Python
csv
模块自动将文件的每一行分割为一个条目列表。默认情况下,它使用逗号

您可能需要提供更多的采样线,因为这完全取决于现有线的格式


使用Python2.7.9进行测试

您是否尝试过
line.split(',')
?模块有什么问题?我没有尝试过CSV模块,因为我一直在尝试更改当前的
流程
定义以适应此要求。我将研究使用csv
csv
您能展示一下您的预期输出吗?脚本是否需要同时处理两个示例输入?@MartinEvans我添加了一个输出示例
import csv

def process(infile, outfile):
    with open(infile, 'rb') as f_input, open(outfile, 'w') as f_output:
        for cols in csv.reader(f_input):
            output = cols[0:3]                          # *GS + 2 numbers
            output.append(cols[4].replace(';', ':'))    # APN
            output.extend(cols[5].rsplit(';', 1))       # IGF and VOW
            output.append(cols[6].replace(';', ':'))    # VWD
            output.append(cols[7].rsplit(';', 1)[0])    # ADC
            f_output.write('\n'.join(output))
            f_output.write('\n')