打开并读取:文件夹python中的多个xml文件 [英] Open and Read : Multiple xml files from the Folder python

查看:1309
本文介绍了打开并读取:文件夹python中的多个xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在一个文件夹中存储了大约150多个XML文件.我想从该文件夹中打开并读取那些XML文件(大约150多个XML文件);之后,我进行下一个分析.我需要在以下代码中进行哪些更改才能从该文件夹中打开/读取多个XML文件?

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

推荐答案

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

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

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

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

这篇关于打开并读取:文件夹python中的多个xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆