【问题标题】:How to run a script python on multiple files如何在多个文件上运行脚本python
【发布时间】:2019-06-19 03:40:38
【问题描述】:

我在一个文件夹中有多个 .txt 文件(file1、file2、file3、file4、file5、....),我需要在大量文件上运行。

我已创建此代码以重新排序并进行一些更改...我想将其应用于原始文件夹(路径)中的多个 txt 文件,并将它们分别保存到另一个文件夹(在最后一个文件夹内)或另一个文件夹中路径

有人可以帮忙吗?

    import pandas as pd
    import numpy as np
    import os
    import glob
    import datetime
    #from datetime import datetime

    df1=pd.read_csv("D:\\Spyder2019\\38A.txt", sep='\s+') 

#############################################33#
#to change name of column and add Temp ms column

    df1=df1.rename(index=str, columns={"Temps":"Heure", "Force":"Force(N)", "Vitesse":"Vitesse(RPM)", "Puissance":"Puissance(w)","Torque":"Torque(N/m)","Angle":"Angle(deg)"})#to change name Temps to Heure

    dtemp=df1['Heure']#to change datetime values into seconds and microseconds in Heure

    dtemp=pd.to_datetime(df1.Heure) #change to datetime values float 

    dtemp1=dtemp.dt.microsecond #dtemp1 object in microseconds with 6 decimals

    dtempms=dtemp1 / 1000000 #dtemp1 object into 2 decimals

    dtemp2=dtemp.dt.second #dtemp2 object in seconds

    df1['Temps_ms']= dtempms + dtemp2 #add second and microseconds to the object

    #################################################
    # to transform the Heure data into sec and microsec

    df1=df1[['Date','NoBille','Heure','Temps_ms','Force(N)','Vitesse(RPM)','Puissance(w)','Torque(N/m)','Angle(deg)']]# reorder the dataframe

    io=df1.iat[0,3]
    #to transform the Heure data into seconds ans microseconds

    df1['Temps(ms)'] = np.where(df1['Temps_ms'] - io <0, df1['Temps_ms'] + 60 - io, df1['Temps_ms'] -io)

    df1=df1.drop(columns=['Temps_ms'])#to eliminate column Temps_ms

    df1=df1[['Date','NoBille','Heure','Temps(ms)','Force(N)','Vitesse(RPM)','Puissance(w)','Torque(N/m)','Angle(deg)']]# to reorder the final dataframe

    ##############################################################################
    print(df1.head())

    df1.to_csv('data1.txt', index = False)

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    如果此脚本可以应用于“输入文件夹”中的所有数据库,则可以将数据库名称作为参数传递给 python 脚本:

    import sys
    
    #...your other imports...
    
    ### take the first argument passed in commmand line as db_name and second as output_file name
    db_name = sys.argv[1]
    output_filename = sys.argv[2]
    df1 = pd.read_csv(db_name, sep='\s+') 
    
    #...rest of your script...
    
    df1.to_csv(output_filename, index = False)
    

    然后 yuo 可以通过外部循环轻松地在输入目录中的文件上调用 python 脚本,例如 ex。在 bash 中:

    mkdir <output-dir>
    ls <input-dir> | while read file; do
        output_filename="<output-dir>/$file.output.txt"
        python <your-script-name> $file $output_filename
    done
    

    它会自动获取文件,操作python脚本并以指定的扩展名将它们写在output-dir中

    【讨论】:

    猜你喜欢
    • 2015-09-03
    • 2021-11-28
    • 2020-05-08
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    相关资源
    最近更新 更多