【问题标题】:Open and Read : Multiple xml files from the Folder python打开并阅读:来自文件夹 python 的多个 xml 文件
【发布时间】:2019-02-14 06:41:35
【问题描述】:

我在一个文件夹中存储了大约 150 多个 XML 文件。我想从该文件夹中打开并读取这些 XML 文件(大约 150 多个 XML 文件);之后,我进行下一个分析。我需要在下面的代码中更改什么才能从该文件夹打开/读取多个 XML 文件?

from bs4 import BeautifulSoup
import lxml
import pandas as pd 

infile = open("F:\\itprocess\\xmltest.xml","r")
contents = infile.read()

【问题讨论】:

  • 您可以使用os.listdir()glob.glob() 查找目录中的所有名称。
  • @AKX 你能举个例子吗?
  • 您可以通过 Google 搜索“stackoverflow glob.glob”来找到它。 stackoverflow.com/questions/3964681/…

标签: python beautifulsoup lxml


【解决方案1】:

os 模块的listdir() 函数是在读取多个文件时使用的好方法。

from bs4 import BeautifulSoup
import lxml
import pandas as pd 
import os    

d = os.listdir()
for file in d:
    infile = open(file,"r")
    contents = infile.read()

当然,这里我假设您的当前目录中只有 XML 文件。

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 2018-01-30
    • 1970-01-01
    • 2014-01-10
    • 2013-10-24
    • 2017-04-24
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多