如何在Python中使用正则表达式从数据集中提取数据?

埃克利尔·汗(Eklil Khan)

我有一个数据集,我想从该数据集中提取正片特征。

در
همین
حال
،
<coref coref_coref_class="set_0" coref_mentiontype="ne" markable_scheme="coref" coref_coreftype="ident">
نجیب
 الله
خواجه
عمری
 ,
 </coref>
<coref coref_coref_class="set_0" coref_mentiontype="np" markable_scheme="coref" coref_coreftype="atr">
سرپرست
وزارت
تحصیلات
عالی
افغانستان
</coref>
گفت
که
در
سه
ماه
گذشته
در
۳۳
ولایت
کشور
<coref coref_coreftype="ident" coref_coref_class="empty" coref_mentiontype="ne" markable_scheme="coref">
خدمات
ملکی
</coref>
از
حدود
۱۴۹
هزار

我想将数据存储在两个列表中的数据集中。find_atr清单中,我将数据存储在coref标签包括的位置coref_coreftype="atr"对于find_ident列表,我想存储的数据。coref_coreftype="ident"因此,在此数据集中的最后一个coref标签上,我们还有另一个coref标签coref_coref_class="empty"我不想存储带有标签的数据coref_coref_class="empty"现在我在正则表达式中提到,它应该只包括那些coref_coref_class="set_.*?"包括在内coref_coref_class="empty"的数据,但它仍然存储的数据coref_coref_class="empty",而应该只存储的数据coref_coref_class="set_.*?"

如何避免:

i_ident = []
j_atr = []
find_ident = re.findall(r'<coref.*?coref_coref_class="set_.*?coref_mentiontype="ne".*?coref_coreftype="ident".*?>(.*?)</coref>', read_dataset, re.S)
ident_list = list(map(lambda x: x.replace('\n', ' '), find_ident))
for i in range(len(ident_list)):
    i_ident.append(str(ident_list[i]))

find_atr = re.findall(r'<coref.*?coref_coreftype="atr".*?>(.*?)</coref>', read_dataset, re.S)
atr_list = list(map(lambda x: x.replace('\n', ' '), find_atr))
#print(coref_list)
for i in range(len(atr_list)):
    j_atr.append(str(atr_list[i]))

print(i_ident)
print()
print(j_atr)
古斯塔沃韦拉斯科

我将您的数据集文件减少为:

A
<coref coref_coref_class="set_0" coref_mentiontype="ne" markable_scheme="coref" coref_coreftype="ident">
B
</coref>
<coref coref_coref_class="set_0" coref_mentiontype="np" markable_scheme="coref" coref_coreftype="atr">
C
</coref>
D
<coref coref_coreftype="ident" coref_coref_class="empty" coref_mentiontype="ne" markable_scheme="coref">
E
</coref>
F

并尝试了这段代码,几乎与您提供的代码相同:

import re

with open ("test_dataset.log", "r") as myfile:
    read_dataset = myfile.read()

i_ident = []
j_atr = []
find_ident = re.findall(r'<coref.*?coref_coref_class="set_.*?coref_mentiontype="ne".*?coref_coreftype="ident".*?>(.*?)</coref>', read_dataset, re.S)
ident_list = list(map(lambda x: x.replace('\n', ' '), find_ident))
for i in range(len(ident_list)):
    i_ident.append(str(ident_list[i]))

find_atr = re.findall(r'<coref.*?coref_coreftype="atr".*?>(.*?)</coref>', read_dataset, re.S)
atr_list = list(map(lambda x: x.replace('\n', ' '), find_atr))
#print(coref_list)
for i in range(len(atr_list)):
    j_atr.append(str(atr_list[i]))

print(i_ident)
print()
print(j_atr)

并得到以下输出,对我来说似乎正确:

[' B ']

[' C ']

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python中使用正则表达式提取数据?

在 Python 中使用正则表达式从字符串中提取数字数据

如何在Python中使用正则表达式从网址中提取某些模式?

如何在Python中使用正则表达式从URL中提取段符?

如何在 python 中使用正则表达式从法律描述中提取信息

如何在python中使用正则表达式从无序数据字符串中提取名称?

如何在Java中使用正则表达式提取HTML的<td>标签数据?

如何在 PySpark 数据帧中提取正则表达式模式的所有实例?

如何在Java中使用正则表达式从符号中提取内容?

如何在Linux Shell中使用正则表达式从文件中提取IP地址?

如何在Linux Shell中使用正则表达式从文件中提取IP地址?

如何在R中使用正则表达式从URL中提取Google表格ID?

如何在 Spark 中使用正则表达式从字符串列中提取日期

如何在java中使用正则表达式从字符串中提取数字

如何在 Google 表格中使用正则表达式从文本中提取时间

如何在python中使用正则表达式从csv文件中获取数据

如何使用正则表达式从字符串值中提取数据?

多个正则表达式模式可使用python从文章中提取数据

正则表达式从刷卡中提取数据

使用正则表达式从pandas数据框中的列中提取数据

正则表达式使用现有数据从ipconfig中提取数据

为什么我使用正则表达式无法从此日志数据中提取数据?

如何在Python中使用正则表达式提取关键字后的数字?

如何在 Python 中使用正则表达式提取指定的匹配项?

如何在python中使用正则表达式提取多个搜索?

如何在python中使用正则表达式删除字母并提取数字?

如何在python中使用正则表达式?

如何在 Java 中使用正则表达式从字符串中提取子字符串?

如何在Android Studio中使用正则表达式从给定的URL中提取子字符串