我最近学到了python来做一些文本提取。我有一个数据集,如下所示:
@article{noauthor_collective_nodate,
title = {Collective teacher efficacy},
abstract = {Overview Influence: Collective teacher efficacy Domain: Teacher Sub-Domain: Teacher attributes Potential to Accelerate Student Achievement: Potential to considerably accelerate Influence Definition: The shared belief by a group of teachers in a particular educational environment that they have the skills to positively impact student outcomes. Evidence Number of meta-analyses: 2 Number of studies: 61 Number of students: 3,489 Number of effects: 61 Effect size: 1.39},
@article{noauthor_collective_nodate,
title = {Collective teacher efficacy},
abstract = {Overview Influence: Collective teacher efficacy Domain: Teacher Sub-Domain: Teacher attributes Potential to Accelerate Student Achievement: Potential to considerably accelerate Influence Definition: The shared belief by a group of teachers in a particular educational environment that they have the skills to positively impact student outcomes. Evidence Number of meta-analyses: 2 Number of studies: 61 Number of students: 3,489 Number of effects: 61 Effect size: 1.39},
}
@article{noauthor_initial_nodate,
title = {Initial teacher education programs},
abstract = {Overview Influence: Initial teacher education programs Domain: Teacher Sub-Domain: Teacher Education Potential to Accelerate Student Achievement: Likely to have small positive impact Influence Definition: Initial teacher education or {ITEs} (sometimes at the undergraduate level and sometimes at the post-graduate level) is the entry-level qualification for teaching in numerous countries, including the United States. More recently, there are school-based {ITEs}, non-accredited {ITEs}, and many online {ITE} programs. Evidence Number of meta-analyses: 5 Number of studies: 117 Number of students: 106,016 Number of effects: 509 Effect size: 0.10},
}
@article{noauthor_professional_nodate,
title = {Professional development programs},
abstract = {Overview Influence: Professional development programs Domain: Teacher Sub-Domain: Teacher Education Potential to Accelerate Student Achievement: Likely to have positive impact Influence Definition: Professional development relates to courses or interventions aimed to enhance the beliefs, actions, impact of knowledge of teachers and school leaders. Evidence Number of meta-analyses: 21 Number of studies: 1,151 Number of students: 2,321,242 Number of effects: 2,938 Effect size: 0.37},
keywords = {Program Development},
}
我想从这篇文章中提取标题和部分摘要。通过使用以下代码,我成功地提取了我想要的输出:
s = "@article{noauthor_collective_nodate, title = {Collective teacher efficacy}, abstract = {Overview Influence: Collective teacher efficacy Domain: Teacher Sub-Domain: Teacher attributes Potential to Accelerate Student Achievement: Potential to considerably accelerate Influence Definition: The shared belief by a group of teachers in a particular educational environment that they have the skills to positively impact student outcomes. Evidence Number of meta-analyses: 2 Number of studies: 61 Number of students: 3,489 Number of effects: 61 Effect size: 1.39},}@article{noauthor_collective_nodate, title = {Collective teacher efficacy}, abstract = {Overview Influence: Collective teacher efficacy Domain: Teacher Sub-Domain: Teacher attributes Potential to Accelerate Student Achievement: Potential to considerably accelerate Influence Definition: The shared belief by a group of teachers in a particular educational environment that they have the skills to positively impact student outcomes. Evidence Number of meta-analyses: 2 Number of studies: 61 Number of students: 3,489 Number of effects: 61 Effect size: 1.39},}"
start = s.find("title = {") + len("title = {")
end = s.find("}, abstract")
start2 = s.find("Influence Definition: ") + len("Influence Definition: ")
end2 = s.find("Evidence Number of meta-analyses:")
substring = s[start:end]
substring2 = s[start2:end2]
print(substring+' - '+substring2+";")
输出:
Collective teacher efficacy - The shared belief by a group of teachers in a particular educational environment that they have the skills to positively impact student outcomes. ;
问题是:
有人能伸出援手吗?
发布于 2021-05-03 13:50:51
这应该可以做到:
with open("myfile.txt", "r") as f:
s = f.readlines()
for x in s:
if x.__contains__("title"):
start = x.find("title = {") + len("title = {")
end = x.find("}")
substring = x[start:end] + " - "
if x.__contains__("Influence Definition"):
start = x.find("Influence Definition: ") + len("Influence Definition: ")
end = x.find("Evidence Number of meta-analyses:")
substring += x[start:end]
print(substring)
print()
f.close()
例如,如果您的文件名为myfile.txt,它将打印以下内容:
集体教师效能--一群教师在特定的教育环境中共同相信他们有能力积极影响学生的结果。 集体教师效能--一群教师在特定的教育环境中共同相信他们有能力积极影响学生的结果。 最初的教师教育计划--最初的教师教育(有时在本科一级,有时在研究生一级)是包括美国在内的许多国家的初级教师资格。最近,有以学校为基础的{ITE}、未经认证的{ITE}和许多在线{ITE}项目. 专业发展方案-专业发展涉及旨在加强教师和学校领导知识的信念、行动和影响的课程或干预措施。
https://stackoverflow.com/questions/67369455
复制相似问题