删除包含字母但不包含内部符号的括号

时间:2017-05-24 07:39:21

标签: python-3.x

    article
      figure
        img     // depicts the antiquity
      figcaption
        header
          h3    // title of the antiquity
          p     // detailed description of the antiquity
        section
          p     // first owner of the antiquity
          p     // second owner of the antiquity
          ...
          p     // last and therfore current owner of the antiquity
        section
          p     // first literature record on this antiquity
          p     // second literature record on this antiquity
          ...
          p     // last literature record on this antiquity

删除括号中的括号,但不删除 括号中有一个符号。 我尝试过re.sub,但它删除了所有括号。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

MenuItem

答案 1 :(得分:0)

在那种情况下(你在评论中说),它可以像下面这样解决。

input_as= ['Hell(o) how a(re) you ? (.)' , 'I a(m) fi(ne) thank you .(.)']
output_as =[]
for input_a in input_as:
    output_a = []
    i = 0
    while i<len(input_a):
        if input_a[i] !="(":
            output_a.append(input_a[i])
            i=i+1
            continue
        else:
            if input_a[i+1].isalpha():
                i=i+1
                while input_a[i]!=")":
                    output_a.append(input_a[i])
                    i =i+1
                i = i+1
            else:
                while input_a[i]!=")":
                    output_a.append(input_a[i])
                    i=i+1
    output_a = "".join(output_a)
    output_as.append(output_a)
print(output_as)