首页 > 解决方案 > 打开文本文件,在两个字符串之间复制数据,转置和连接

问题描述

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

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'and ' id: standardize'。最后,我想用逗号转置和连接每一行。基本上,我希望它看起来像下面的这张图片。

在此处输入图像描述

这是我正在使用的代码。

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

看起来应该很接近,但这里有些东西,我不知道是什么。

标签: pythonpython-3.x

解决方案


如果你的开始和结束总是相同的,你可以使用正则表达式来识别两者之间的所有内容,然后将它们构建到一个列表中,最后将它们保存到你的目的地。

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

推荐阅读