打开文本文件,在两个字符串之间复制数据,转置并加入

时间:2018-10-22 15:35:11

标签: python python-3.x

我有一个看起来像这样的数据集。

random stuff
more random stuff
random, random, random
random, random, random
  - class: pipe.steps.standardize.Standardize
    conf:
      schema_def:
        fields:
        - data_type: STRING
          name: Operation
        - data_type: STRING
          name: SNL_Institution_Key
        - data_type: INTEGER
          name: SNL_Funding_Key
        - data_type: STRING
          name: CUSIP
    id: standardize
  steps:
  steps:
  steps:
  - class: pipe.steps.standardize.Standardize
    conf:
      schema_def:
        fields:
        - data_type: STRING
          name: Operation
        - data_type: INTEGER
          name: Rate_Value_OID
        - data_type: INTEGER
          name: Key_Rate
    id: standardize
  steps:
  steps:
  steps:

我正在尝试复制/粘贴' - class: pipe.steps.standardize.Standardize'' id: standardize'之间的所有数据。我想包括' - class: pipe.steps.standardize.Standardize'' id: standardize'。最后,我想转置并连接每行以及逗号。基本上,我希望它看起来像下面的图像。

enter image description here

这是我正在使用的代码。

import itertools

with open('C:\\Users\\ryans\\OneDrive\\Desktop\\AllYAML\\final_result.txt', 'r') as f, open('C:\\Users\\ryans\\OneDrive\\Desktop\AllYAML\\test_out.txt', 'w') as fout:
    while True:
        for line in f:
            line = line.strip(" -")
            s = line.split(": ")
            fout.write(": ".join(s[::-1]))
        it = itertools.dropwhile(lambda line: line.strip() != '- class: pipe.steps.standardize.Standardize', f)
        if next(it, None) is None: break
        fout.writelines(itertools.takewhile(lambda line: line.strip() != '- class: pipe.steps.load.Load', it))

似乎应该很接近,但是这里有些问题,我不知道是什么。

1 个答案:

答案 0 :(得分:1)

如果您的起点和终点总是一样,则可以使用正则表达式来标识介于两者之间的所有内容,然后将它们构建到列表中,最后将它们保存到目的地。

with open('final_result.txt','r') as f:
    lines = f.read()

start = '-class:pipe.steps.standardize.Standardize,'
end = ',id:standardize'

import re
results = re.findall(r'- class: pipe\.steps\.standardize\.Standardize\n    (.*?)id: standardize',lines,flags=re.DOTALL)
prep_results = [i.replace(' ','').split('\n') for i in results]
output = [start+','.join(i)+end for i in prep_results]

with open('final_results.txt','w') as f:
    for line in output:
        f.write("%s\n" % line)

输出:

for i in output:
    print(i)
>>
-class:pipe.steps.standardize.Standardize,conf:,schema_def:,fields:,-data_type:STRING,name:Operation,-data_type:STRING,name:SNL_Institution_Key,-data_type:INTEGER,name:SNL_Funding_Key,-data_type:STRING,name:CUSIP,,id:standardize
-class:pipe.steps.standardize.Standardize,conf:,schema_def:,fields:,-data_type:STRING,name:Operation,-data_type:INTEGER,name:Rate_Value_OID,-data_type:INTEGER,name:Key_Rate,,id:standardize