将函数应用于文件路径列表并将 csv 输出写入相应路径

Applying function to a list of file-paths and writing csv output to the respective paths

如何将一个函数应用于我构建的文件路径列表,并在同一路径中写入输出 csv?

read file in a subfolder -> perform a function -> write file in the subfolder -> go to next subfolder

#opened xml by filename
with open(r'XML_opsReport 100001.xml', encoding = "utf8") as fd:
    Odict_parsedFromFilePath = xmltodict.parse(fd.read()) 

#func called in func below
def activity_to_df_one_day (list_activity_this_day): 
    ib_list = [pd.DataFrame(list_activity_this_day[i], columns=list_activity_this_day[i].keys()).drop("@uom") for i in range(len(list_activity_this_day))]
    return pd.concat(ib_list)

#Processes parsed xml and writes csv 
def activity_to_df_all_days (Odict_parsedFromFilePath, subdir): #writes csv from parsed xml after some processing
    nodes_reports = Odict_parsedFromFilePath['opsReports']['opsReport']
    list_activity = []
    for i in range(len(nodes_reports)):
        try:
            df = activity_to_df_one_day(nodes_reports[i]['activity'])
            list_activity.append(df)

        except KeyError:
            continue
    opsReport = pd.concat(list_activity)
    opsReport['dTimStart'] = pd.to_datetime(opsReport['dTimStart'], infer_datetime_format =True)
    opsReport.sort_values('dTimStart', axis=0, ascending=True, inplace=True, kind='quicksort', na_position='last')
    opsReport.to_csv("subdir\opsReport.csv") #write to the subdir




def scanfolder(): #fetches list of file-paths with desired starting name.

    list_files = []

    for path, dirs, files in os.walk(r'C:\..\xml_objects'): #directory containing several subfolders
        for f in files:
            if f.startswith('XML_opsReport'):
                list_files.append(os.path.join(path, f))
    return list_files

filepaths = scanfolder() #list of file-paths  

每个功能都很好,xml处理的很好,所以我不分享xml结构。 filepaths 中有 100 多个路径,每个路径都是不同的子目录。我希望将来也能够应用上述流程,在那里我可以获得文件路径并执行所需的操作。将 csv 文件写入其子目录很重要。

要获取文件所在的目录,您可以使用:

import os

for root, dirs, files, in os.walk(some_dir):
    for f in files:
        print(root)
        output_file = os.path.join(root, "output_file.csv")
        print(output_file)

这就是你要找的吗?

输出:

somedir
somedir\output_file.csv

另见 and Find current directory and file's directory

能够用 os.path.join 解决。

    exceptions_path_list =[]
    for i in filepaths:
        try:
            with open(i, encoding = "utf8") as fd:
                doc = xmltodict.parse(fd.read())
                activity_to_df_all_days (doc, i)
        except ValueError:
            exceptions_path_list.append(os.path.dirname(i))
            continue

    def activity_to_df_all_days (Odict_parsedFromFilePath, filepath):
        ...
        ...
        ...    
        opsReport.to_csv(os.path.join(os.path.dirname(filepath), "opsReport.csv"))