python连接带有特定字符串的行

标记

我有一个代码输出的 XML 字符串。从输出中,我想加入两行,其中有一个'predicted_serotype'字符串(这个字符串被称为diff_string):

 <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:
            diff_list[n] = "\n"+line.strip()
        else:
            diff_list[n]=line.rstrip()

    print("\n".join(diff_list))

我想要的是这样的输出:

<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>
马克西姆

这应该可以解决问题,我们寻找第一次出现的带有 predict_serotype 的行,然后如果遇到另一个出现,我们将其删除并将其添加到第一次出现。

diff_list = diff.split("\n")
first_serotype_line_nbr = 0

for n,line in enumerate(diff_list):
    if "predicted_serotype" in line:
        if first_serotype_line_nbr == 0:
            first_serotype_line_nbr = n
        else:
            diff_list[first_serotype_line_nbr] += line.rstrip()
            del(diff_list[n])
    else:    
        diff_list[n]=line.rstrip()

print("\n".join(diff_list))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

连接特定的字符串索引python

带有连接的搜索查询在搜索的字符串为空时显示所有行

仅保留带有特定字符串的行上方的行

如何使用Python合并或连接带有字符串的两个熊猫数据框

mysql SELECT from带有连接字符串的列

连接带有字符串的字体真棒图标

如何在类python中按特定子字符串查找带有类字符串的span

使用带有regexp(json字符串)的特定值从mysql获取行

给带有特定字符串的sed文件中的每一行添加#

在带有特定单词的行后获取字符串

使用for循环使用带有if语句的列表枚举来搜索特定字符串的行

如何从文件中删除带有特定字符串的行

解析带有某些特定关键字的字符串以进行拆分(在字符串文字之外),但在 Python 中未在字符串文字内拆分

htaccess删除带有查询字符串的特定地址

如何过滤带有特定字符串的txt文件

查找带有特定日期字符串的文件

在 Python 中打印带有特殊字符的字符串

Python Pandas 搜索带有元字符的字符串

在Python中的特定位置连接字符串

带有 sphinx 新行的 Python 自动文档字符串

如何在带有sed或awk的行之间选择特定的字符串,而不打印整个行

从1行获取带有awk的字符串/短语

带有新行的按键字符串

选择带有包含括号的字符串索引的行

在每一行中创建一个带有特定字符串的新列

在特定字符串开始之前删除所有行 python

使用Python在文件中查找具有特定字符串的行

在 Python 中提取具有特定字符串的行的更好方法

Python - 带有字符串文字的 bytearray 函数