python - 如何使用python根据字段值从xml部分中提取内容
问题描述
我想根据字段类型的值从 xml 文件中提取内容。基本上,它是一个 json 文件,我将其转换为 xml。该文件具有字段“body”、“id”、“type”和片段。如果'type='summary',我想提取所有这些字段的内容。我所做的代码如下:
def load_extract(data):
path=""
soup = BeautifulSoup(open(path),"html.parser")
q1=[]
qtype=[]
snippets=[]
for q in soup.findAll('body'):
q=q.text
q1.append(q)
for types in soup.findAll('type'):
type1=types.text
qtype.append(type1)
snippets=soup.findAll('snippets')
summary_ids=[]
summary_dict=[]
for i in range (0, len(qtype)):
print "extracting the summary type question"
if qtype[i]=='summary':
summary_ids.append(i)
for j in summary_ids:
summary_dict.append({q1[j]:snippets[j]})
return summary_dict
该代码在我运行的小集合上运行良好,但对于大集合,len(q1) 不等于 len(snippets)。这就产生了一个问题。我不知道训练数据是否真的没有一些身体的片段。但这会在映射和提取方面产生问题。我在想是否可以只提取 type='summary' 的正文、id 和片段。请求您的帮助!
解决方案
推荐阅读
- ruby-on-rails - debase gem 在安装时在我面前爆炸
- java - 引用不同文件中的 avro 模式不起作用 - avro 引发错误
- flutter - 颤振我正在尝试向我的应用程序添加一个 tabbarview,但没有任何效果,我想要一个标签来显示来自巴西和另一个来自世界的案例
- unity3d - 如何实时变形蒙皮网格?
- mongodb - MongoDB 分片和复制
- python - 为什么我在 pyhton 统计信息中得到一个“int”对象不可迭代错误?
- python - 如何使用包含引号并使用多个分隔符的 pandas 读取 CSV 文件
- java - Selenium 在忽略配置文件偏好的同时启动 Firefox
- node.js - 如何正确发出事件?
- r - R函数或循环用于重复选择满足条件的行,另存为单独的对象,以及重命名列标题