【问题标题】:Make new txt file with size info of output and input files使用输出和输入文件的大小信息制作新的 txt 文件
【发布时间】:2019-06-02 03:33:18
【问题描述】:

上面的代码部分很好,但是第二部分我试图创建一个新的 txt 文件,其中包含有关在第一部分中创建的文件的信息,例如在这个 txt 文件中将写入:INPUT FILE1 SIZE IS 42, OUTPUT FILE1 SIZE IS 324, 比第二个文件:INPUT FILE2 SIZE IS 62, OUTPUT FILE1 SIZE IS 543...等

将熊猫导入为 pd

导入全局

导入操作系统

文件 = glob.glob('*.csv')

对于文件中的文件:

df = pd.read_csv(file, header= None)

df1 = df.iloc[:, :4].agg(['sum','max','std'])

df1.columns = range(1, len(df1.columns) + 1)

s = df1.stack()

L = ['{} of the {}. column is {}'.format(a, b, c) for (a, b), c in s.items()]

output_file_name = "output_" + file

pd.Series(L).to_csv(output_file_name ,index=False)#this part is good

对于文件中的文件:

with open(file + "stats.txt", 'a+') as f:

    f.write(' input file size is {}'.format(os.path.getsize(file)))

f.write('输出文件大小为{}'.format(os.path.getsize(output_file_name)))

f.close()

【问题讨论】:

    标签: python-3.x pandas dataframe glob


    【解决方案1】:

    用途:

    import glob, os
    import pandas as pd
    
    files = glob.glob('*.csv')
    
    #loop by all files
    for file in files:
        L = []
        #remove not starting by output_
        if not file.startswith(('output_','file_size_')):
            output_file_name = "output_" + file
            #add both format
            infile = 'SIZE OF INPUT FILE {} IS {}, '.format(file, os.path.getsize(file))
            outfile = 'SIZE OF INPUT FILE {} IS {}'.format(output_file_name, 
                                                           os.path.getsize(output_file_name))
            #join together and append to list
            L.append(infile + outfile )
    
            #create Series and write to file
            pd.Series(L).to_csv('file_size_{}'.format(file), index=False)
    

    【讨论】:

    • 太好了,现在我有一个 file_size.txt,但我想为文件夹中的每个输入单独制作这个文件。很好的说明!!
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多