[英]If string in one file matches string in another, print line and next line
[英]if string in line and next line
我有一個 xml 字符串(轉換為列表),我正在尋找一個特定的字符串。 僅當此字符串在列表的下一行中具有相同的特定字符串時,我才想做一些事情。
xml(稱為差異):
<result type="MLST" value="96">
<result_data type="profile" value="43,47,49,49,41,15,3"/>
<result_data type="QC_minimum_consensus_depth" value="7"/>
<result_data type="QC_max_percentage_non_consensus_base" value="10.0"/>
<result_data type="QC_percentage_coverage" value="100"/>
<result_data type="QC_minimum_consensus_depth_for_all_loci" value="7,17,27,10,25,18,22" diff:update-attr="value:7,17,27,10,24,18,22"/>
<result_data type="QC_complete_pileup" value="TRUE"/>
<result_data type="QC_mean_consensus_depth" value="17.67"/>
<result_data type="QC_max_percentage_non_consensus_base_for_all_loci" value="10.0, 6.25, 3.45, 9.09, 5.88, 5.26, 5.41"/>
<result_data type="QC_mean_consensus_depth_for_all_loci" value="17.67, 32.49, 34.09, 23.44, 35.57, 29.02, 39.08" diff:update-attr="value:17.67, 32.49, 34.09, 23.44, 34.24, 29.02, 39.08"/>
<result_data type="QC_traffic_light" value="GREEN"/>
<result_data diff:insert="" type="predicted_serotype" diff:add-attr="type;value" value="('Schwarzengrund (Achtman)', 168), ('Schwarzengrund (PHE)', 83), ('Blockley (Achtman)', 1), ('Uppsala (Achtman)', 1), ('Oslo (Achtman)', 1), ('Schwarzengru (Achtman)', 1), ('Iv Rough:Z4,Z32:- (Achtman)', 1)"/>
<result_data type="predicted_serotype" value="('Schwarzengrund (PHE)', 13)" diff:delete=""/>
</result>
<gastro_prelim_st reason="not novel" success="false">
<type st="96"/>
</gastro_prelim_st>
代碼:
diff_list = diff.split("\n")
for n,line in enumerate(diff_list):
if "predicted_serotype" in line:
print(line)
我想要的是,如果你在一行中罰款“predicted_serotype”,而下一行也有“predicted_serotype”,然后打印。
感謝任何幫助。
我所做的,只是將您的 xml 內容復制到一個 txt 文件中,然后將其作為字符串讀取
file = "path/tmp.txt"
# the content will be a variable containing string
with open(file, 'r') as file:
content = file.read()
# diff_list is a list
diff_list = content.split("\n")
for n,line in enumerate(diff_list):
print(n)
if "predicted_serotype" in line and "predicted_serotype" in diff_list[n+1]:
print(line)
diff_list
基本上是一個列表,因此您可以執行各種索引操作。
同樣正如評論中提到的其他人,請確保n+1
未超出范圍
更新的@bruno desthuilliers 建議:
for line, next_line in zip(diff_list, diff_list[1:]):
if "predicted_serotype" in line and "predicted_serotype" in next_line:
print(line)
這樣你就可以避免索引錯誤
盡管我的回答與字面上的問題無關,但考慮到問題的上下文,我建議使用如下正則表達式。
import re
diff = "Your xml text"
regx = re.compile("(<.*predicted_serotype.*\/>)\s.*predicted_serotype.*")
matches = regx.findall(diff)
for match in matches:
print(match)
此處,正則表達式匹配包含字符串“predicted_serotype”的兩行,但regx.findall
僅返回括號內的捕獲組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.