文件目录列表或df的动态重复

2024-09-29 23:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有工作代码来搜索我想要的目录中的文件。我可以将它们保存为数据帧或列表。然后我了解了如何使用findall(),find().text从一个xml文件中获取标记和文本。现在,我想让它对一组与此数据帧或列表中的位置相等的值更加动态我收到一条错误消息“应该是str、bytes或os.PathLike object,而不是tuple”。我想知道我的理解在这段代码中哪里是错误的,或者我的理解在哪里有点不正确?我也不确定我是否对数据帧过于复杂,应该只使用一个列表。 .....

import os  
import pandas as pd
import os  
import xml.etree.ElementTree as ET

.....
current_dur = r'Workplace Investing'
#empty data frame to put the file paths in.
file_results = []    

#logic to search through the directories.
for root, dirs, files in os.walk(current_dur):
    for file in files:
            if file.endswith('.ldm') or file.endswith('.cdm') or file.endswith('.pdm'):
                file_results.append(os.path.join(root, file))           

filesdataframe = pd.DataFrame(file_results)
filesdataframe.rename(columns = { 0 :'Directory Path'}, inplace = True)

......


#In my working code I set the xmlfile equal to a single directory path.
#This worked for one file. Now my thinking is maybe I set xmlfile equal to dataframe.iterrows().
#That way when I make my loop it will go by each row. 
xmlfile = next(filesdataframe.iterrows())

for ind in filesdataframe.index:
    #This is next line is where I am getting my issue. 
    tree = ET.parse(xmlfile)
   
    root = tree.getroot()
.....

Tags: theto数据inimport列表foros

热门问题