python - 打开文本文件,在两个字符串之间复制数据,转置和连接
问题描述
我有一个看起来像这样的数据集。
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))
看起来应该很接近,但这里有些东西,我不知道是什么。
解决方案
如果你的开始和结束总是相同的,你可以使用正则表达式来识别两者之间的所有内容,然后将它们构建到一个列表中,最后将它们保存到你的目的地。
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
推荐阅读
- angular - 以角度显示或渲染另一个组件内的一个组件的最佳实践是什么?
- mongodb - 基于两个集合之间的正则表达式搜索匹配查找和合并对象
- django - 在 django-storages 中将 Amazon S3 与基于欧洲的存储桶一起使用的问题
- css - mat-card 的内容不适合 mat-card
- java - Postgres & JPA 命名查询 - WHERE 中的任何 (*) 值
- javascript - 使用 axios 的 Google API 批处理请求
- json - 在 swift 代码应用程序中获取和解析 JSON
- reactjs - 使用 react-pdf/renderer 库创建 pdf 客户端
- android - Recyclerview在将图像存储在imageview中时第二次显示异常
- jmeter-5.0 - 测试开始时覆盖读入内存的 CSV 配置元素数据