所以我遇到了一个问题,我一直在解析一个XML文件,如下所示:
soup = BeautifulSoup(xml_string, "lxml")
pub_ref = soup.findAll("publication-reference")
with open('./output.csv', 'ab+') as f:
writer = csv.writer(f, dialect = 'excel')
for info in pub_ref:
assign = soup.findAll("assignee")
pat_cite = soup.findAll("patcit")
for item1 in assign:
if item.find("orgname"):
org_name = item.find("orgname").text
for item2 in pat_cite:
if item2.find("name"):
name = item2.find("name").text
for inv_name, pat_num, cpc_num, class_num, subclass_num, date_num, country, city, state in zip(soup.findAll("invention-title"), soup.findAll("doc-number"), soup.findAll("section"), soup.findAll("class"), soup.findAll("subclass"), soup.findAll("date"), soup.findAll("country"), soup.findAll("city"), soup.findAll("state")):
writer.writerow([inv_name.text, pat_num.text, org_name, cpc_num.text, class_num.text, subclass_num.text, date_num.text, country.text, city.text, state.text, name])
我只限于几个元素(如末尾的文本条目所示),但是现在我有大约10个父元素和30多个子元素需要解析,所以像这样显式地声明它们将不再有效。另外,我在数据中有重复,看起来像:
^{pr2}$我希望能够将重复的子根(如patcit)解析到CSV文件中,列如下:
invention name country city .... patcit name1 patcit date1....
white space patcit name2 patcit date2....
white space patcit name2 patcit date3....
以此类推……因为每项发明都有不止一个引文或参考文献,所以大多数其他信息只有一列。在
试试下面的脚本。我想这就是你想要的。在
结果:
^{pr2}$此解决方案适用于您稍后提供的链接:
相关问题 更多 >
编程相关推荐