简体   繁体   English

如果字符串在一行和下一行

[英]if string in line and next line

I have a xml string (converted to a list) and I am looking for a specific string.我有一个 xml 字符串(转换为列表),我正在寻找一个特定的字符串。 I want to do stuff only if this string has the same specific string in the next line in the list.仅当此字符串在列表的下一行中具有相同的特定字符串时,我才想做一些事情。

xml (called diff): 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>

Code:代码:

diff_list = diff.split("\n")    
    for n,line in enumerate(diff_list):
        if "predicted_serotype" in line:
            print(line)

What I want is if you fine "predicted_serotype" in line and the next line has also "predicted_serotype" then print.我想要的是,如果你在一行中罚款“predicted_serotype”,而下一行也有“predicted_serotype”,然后打印。

Appreciate any help.感谢任何帮助。

What I did, just copied your xml content into a txt file and then read it as a string我所做的,只是将您的 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)

basically diff_list is a list, so you can do all sort of indexing operations. diff_list基本上是一个列表,因此您可以执行各种索引操作。

Also as others mentioned in the comments, make sure n+1同样正如评论中提到的其他人,请确保n+1

is not out of range未超出范围

UPDATED @bruno desthuilliers suggested:更新的@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)

This way you avoid the index error这样你就可以避免索引错误

Though my answer is not relevant to the question literally, considering the context of the question, I would suggest using regular expression as below.尽管我的回答与字面上的问题无关,但考虑到问题的上下文,我建议使用如下正则表达式。

import re

diff = "Your xml text" 
regx = re.compile("(<.*predicted_serotype.*\/>)\s.*predicted_serotype.*")
matches = regx.findall(diff)

for match in matches:
    print(match)

Here, the regex matches two lines containing the string "predicted_serotype" but regx.findall returns only the capture group inside the parenthesis.此处,正则表达式匹配包含字符串“predicted_serotype”的两行,但regx.findall仅返回括号内的捕获组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM