使用 beautifulsoup 将 xml 个文件解析为 csv 文件
Parsing xml files to csv file using beautifulsoup
我正在尝试解析多个(最终超过 1000 个)xml 文件以获取三个信息 persName、@ref 和 /date。我已经设法获取了所有文件,当我使用 print() 时,它为我提供了我想要的所有信息。但是,当我尝试将该信息写入 csv 文件时,仅解析最后一个 xml 文件。
from bs4 import BeautifulSoup
import csv
import os
path = r'C:\programming1\my-app'
for filename in os.listdir(path):
if filename.endswith(".xml"):
fullpath = os.path.join(path, filename)
f = csv.writer(open("test2.csv", "w"))
f.writerow(["date", "Name", "pref"])
soup = BeautifulSoup (open(fullpath, encoding="utf-8"), "lxml")
# removing unnecessary information to better isolate //date
for docs in soup.find_all('tei'):
for pubstmt in soup.find_all("publicationStmt"):
pubstmt.decompose()
for sourdesc in soup.find_all("sourceDesc"):
sourdesc.decompose()
for lists in soup.find_all("list"):
lists.decompose()
for heads in soup.find_all("head"):
lists.decompose()
#finding all dates of Protokolls under /title
for dates in soup.find_all("date"):
date = dates.get('when')
#getting all Names from xml files exept for thos in /list
for Names in soup.find_all("persname"):
nameonly = Names.contents
nameref = Names.get("ref")
f.writerow([date, nameonly, nameref])'
如果我将 writerow 放在名称下方,那么它只会写入最后一个文件的所有信息,如果我将 writerow 放在名称后面,那么它只会写入一个名称的信息
有人能告诉我我做错了什么吗?我已经尝试了很多 for 循环并且 none 似乎有效。
您写道:
However when I try to write that information to a csv file only the last xml file is parsed.
通过阅读您的代码,发生的事情是:
every XML is parsed, but only the last XML file is written to the CSV
那是因为您要为每个输入 XML 打开 test2.csv“用于写入”。当您打开写入时,"w"
,它会创建文件,或者在您的情况下,它会 重新-为每次迭代创建文件(覆盖其内容)。
因为你想要 header:
- 您需要在开始迭代 XMLs
之前打开 CSV
- 写下你的header
- 遍历您的 XML 处理并写入 CSV
- 在最底部,退出循环后,关闭 CSV
我正在尝试解析多个(最终超过 1000 个)xml 文件以获取三个信息 persName、@ref 和 /date。我已经设法获取了所有文件,当我使用 print() 时,它为我提供了我想要的所有信息。但是,当我尝试将该信息写入 csv 文件时,仅解析最后一个 xml 文件。
from bs4 import BeautifulSoup
import csv
import os
path = r'C:\programming1\my-app'
for filename in os.listdir(path):
if filename.endswith(".xml"):
fullpath = os.path.join(path, filename)
f = csv.writer(open("test2.csv", "w"))
f.writerow(["date", "Name", "pref"])
soup = BeautifulSoup (open(fullpath, encoding="utf-8"), "lxml")
# removing unnecessary information to better isolate //date
for docs in soup.find_all('tei'):
for pubstmt in soup.find_all("publicationStmt"):
pubstmt.decompose()
for sourdesc in soup.find_all("sourceDesc"):
sourdesc.decompose()
for lists in soup.find_all("list"):
lists.decompose()
for heads in soup.find_all("head"):
lists.decompose()
#finding all dates of Protokolls under /title
for dates in soup.find_all("date"):
date = dates.get('when')
#getting all Names from xml files exept for thos in /list
for Names in soup.find_all("persname"):
nameonly = Names.contents
nameref = Names.get("ref")
f.writerow([date, nameonly, nameref])'
如果我将 writerow 放在名称下方,那么它只会写入最后一个文件的所有信息,如果我将 writerow 放在名称后面,那么它只会写入一个名称的信息
有人能告诉我我做错了什么吗?我已经尝试了很多 for 循环并且 none 似乎有效。
您写道:
However when I try to write that information to a csv file only the last xml file is parsed.
通过阅读您的代码,发生的事情是:
every XML is parsed, but only the last XML file is written to the CSV
那是因为您要为每个输入 XML 打开 test2.csv“用于写入”。当您打开写入时,"w"
,它会创建文件,或者在您的情况下,它会 重新-为每次迭代创建文件(覆盖其内容)。
因为你想要 header:
- 您需要在开始迭代 XMLs 之前打开 CSV
- 写下你的header
- 遍历您的 XML 处理并写入 CSV
- 在最底部,退出循环后,关闭 CSV