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

时间:2018-11-20 16:36:47

标签: python-3.x pandas os.path

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

  

在子文件夹中读取文件->执行功能->在子文件夹中写入文件   子文件夹->转到下一个子文件夹

#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文件写入其子目录很重要。

2 个答案:

答案 0 :(得分:1)

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

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

另请参阅Python 3 - travel directory tree with limited recursion depthFind current directory and file's directory

答案 1 :(得分:0)

能够用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"))