简体   繁体   English

循环多个文件以根据其名称合并

[英]Loop over multiple files to merge according their names

I'm a new python. 我是新蟒蛇。 I have for loop function gives me a folder contains 100 files "the data inside is numbers and the nuber of raws are the same" like the follow: 我有for循环功能,给我一个包含100个文件的文件夹,“其中的数据是数字,而原始数字则相同”,如下所示:

A_0.20_1_.txt        for example   A_0.20_1_    B_0.20_1_      
B_0.20_1_.txt                         1            4
A_0.20_2_.txt                         2            5
B_0.20_2_.txt                         3            6

A_0.40_1_.txt
B_0.40_1_.txt
A_0.40_2_.txt
B_0.40_2_.txt

and so on.....

These files saved in a folder named output I need to merge the two files form the output folder into one file like: 这些文件保存在名为output的文件夹中,我需要将输出文件夹中的两个文件合并为一个文件,例如:

merged_A_B_0.20_1_.txt       for example   merged_A_B_0.20_1_
merged_A_B_0.20_2_.txt                        1    4
                                              2    5
merged_A_B_0.40_1_.txt                        3    6
merged_A_B_0.40_2_.txt

and so on.....

I tried to use the following code: 我尝试使用以下代码:

filename_list = [f for f in os.listdir(r'C:\Users\output\')if os.path.isfile(f)] 
columns = []
for filename in filename_list:
    f=open(filename)
    x = np.array([float(raw) for raw in f.readlines()])
    columns.append(x)
columns = np.vstack(columns).T
np.savetxt('filename_out.txt', columns) 

But it doesn't work and give me error 但这不起作用并给我错误

     Traceback (most recent call last):

File "<ipython-input-6-5df3067f04e7>", line 1, in <module>
runfile('C:/Users/user/Downloads/combine 2 files new2.py', wdir='C:/Users/user/Downloads')

File "C:\ProgramData\Anaconda3\lib\site-
 packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-
  packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/user/Downloads/combine 2 files new2.py", line 22, in 
<module> columns = np.vstack(columns).T

File "C:\ProgramData\Anaconda3\lib\site-
packages\numpy\core\shape_base.py", line 230, in vstack
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)

ValueError: need at least one array to concatenate    

Please, any help? 请帮忙吗?

Your code is actually working the problem is you don't pass a full path to os.path.isfile() so it does not return True and your list of files is empty 您的代码实际上是有效的问题是您没有将完整路径传递给os.path.isfile()因此它不会返回True并且文件列表为空

import numpy as np
import os
file_path = r"C:\Users\output"
filename_list = []
for file in os.listdir(file_path):
    file = os.path.join(file_path, file)
    if os.path.isfile(file):
        filename_list.append(file)

columns = []
for filename in filename_list:
    with open(filename, 'r') as f:
        x = np.array([float(raw) for raw in f.readlines()])
        columns.append(x)
columns = np.vstack(columns).T
np.savetxt('filename_out.txt', columns)

This writes the data of all files into ONE file with one column for each file 这会将所有文件的数据写入一个文件,每个文件一列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM