Open and Read : Multiple xml files from the Folder python

穿精又带淫゛_ 提交于 2019-12-13 04:23:00

问题


I have stored about 150+ XML files in one folder. I want to open and read those XML files from that folder (about 150+ XML files); after that, I do the next analysis. What do I need to change in the below code to open/read the multiple XML files from that folder?

from bs4 import BeautifulSoup
import lxml
import pandas as pd 

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

回答1:


os module's listdir() function is a good way to use while reading multiple files.

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()

Of course here I am assuming you only have your XML files in your current directory.



来源:https://stackoverflow.com/questions/52247468/open-and-read-multiple-xml-files-from-the-folder-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!