提取python中小括号之间字符串的特定部分

2024-09-30 02:27:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以整数开始的数据,然后是字符串,但在某些情况下,还有字符串的特殊标记。例如:8((NP 8.1جے
8.2پوС
)) 为此,我尝试访问并加入一行

>filter_list = [] #empty 
>list huge_list = []   #empty list
>with open('filename.txt','r', encoding="utf8") as f: #read file 
>huge_list = f.readlines() #read line by line 
>for i in huge_list:
    >x = i[i.find("\t((\tNP\t")+1: i.find("))")]     ** #try to extract string like 8   ((  NP  <ne=NEL>
       8.1  جے      
       8.2  پور     
       ))**
       >if (x not in range(0)):
         >filter_list.append(x)

>print(filter_list) 
>Output: [''<Sentence id="null">', '0\t((\tSSF\t',  '1\tگلابی\t\t', 
 '2\tشہر\t\t', '3\tکے\t\t', '4\tنام\t\t', '5\tسے\t\t',
  '6\tمشہور\t\t', '7\t،\t\t', '8((\tNP\t<ne=NEL>', '8.1\tجے\t\t',  
  '8.2\tپور\t\t', '\t', '9\t،\t\t', '((\tNP\t<ne=NEL>','</Sentence>'
>**Desired Output:** 8((\tNP\t<ne=NEL>', '8.1\tجے\t\t', '8.2\tپور\t\t')) 


enter code here

Tags: 字符串inreadoutputnplinefindfilter

热门问题