【问题标题】:Folder name as one of the column names文件夹名称作为列名称之一
【发布时间】:2016-09-13 10:50:27
【问题描述】:

我在 100 多个文件夹中有 1000 多个文件。我需要将文件夹名称之一写入文件作为列之一。

目录结构:

Data -> 000 -> Trajectory -> set of files
Data -> 001 -> Trajectory -> set of files
Data -> 002 -> Trajectory -> set of files
Data -> 003 -> Trajectory -> set of files
.        .        .
.        .        .
.        .        .
Data -> nnn -> Trajectory -> set of files

每个 Trajectory 文件夹都有 100 多个文件,每个文件都有以下列。每个文件都有一个扩展名.plt

39.984702,116.318417,0,492,39744.1201851852,2008-10-23,02:53:04
39.984683,116.31845,0,492,39744.1202546296,2008-10-23,02:53:10
39.984686,116.318417,0,492,39744.1203125,2008-10-23,02:53:15
39.984688,116.318385,0,492,39744.1203703704,2008-10-23,02:53:20
39.984655,116.318263,0,492,39744.1204282407,2008-10-23,02:53:25
39.984611,116.318026,0,493,39744.1204861111,2008-10-23,02:53:30

我想要将文件夹名称作为列名之一。

预期输出:文件夹中名称为000的文件

000 39.984702,116.318417,0,492,39744.1201851852,2008-10-23,02:53:04
000 39.984683,116.31845,0,492,39744.1202546296,2008-10-23,02:53:10
000 39.984686,116.318417,0,492,39744.1203125,2008-10-23,02:53:15
000 39.984688,116.318385,0,492,39744.1203703704,2008-10-23,02:53:20
000 39.984655,116.318263,0,492,39744.1204282407,2008-10-23,02:53:25
000 39.984611,116.318026,0,493,39744.1204861111,2008-10-23,02:53:30

我在附近找不到任何可以解决的样本。任何建议都会有所帮助。

编辑 1: 正如 @EdChum 关于使用 glob 所建议的那样,但这只能让我找到具有给定扩展名的文件。但我这里的问题是别的。

用更简单的话

rootdir -> subdir_1 -> subdir_2 -> files

subdir_1 的名称作为col[0] 包含在subdir_2 以及其他列中存在的所有文件中。无需创建新的输出文件即可追加文件。

【问题讨论】:

  • 请提供您对该问题的尝试
  • 坦率地说,我一直在寻找如何开始,但我还没有看到一个这样的例子可以读取目录名称并将文件作为列之一:(因此问题没有任何代码尝试。
  • 你想使用glob然后解析路径得到文件夹名并在加载每个文件后添加一个新列df['folder_name'] = folder_name,请试一试

标签: python file pandas directory dataset


【解决方案1】:
  • 第一段代码将获取所有以.plt结尾的文件
  • 接下来,我们检查您的 subdir_1 是否实际上仅由数字组成并且是字符长(只是一些健全性检查以确保我们不会命中所有以 .plt 结尾的文件)以及 plt 文件是否为在轨迹文件夹中。
  • 最后,打开了一个与原始文件同名的新文件,但附加了.new。读取旧文件中的每一行,在开头添加一个带有目录名称的新列,并将新行写入输出文件。


import os

#get all plt files
traj_files = []
for root, dirs, files in os.walk('Data'):
    for filename in files:
        if filename.endswith('.plt'):
            traj_files.append(os.path.join(root, filename))

for traj_file in traj_files:

    #the new column we want to write
    new_col = traj_file.split('/')[1]
    #check if filename looks OK
    if len(new_col) != 3 or not new_col.isnumeric() or not '/Trajectory/' in traj_file:
        continue

    #read old file and write new column
    with open(traj_file + '.new', 'w') as new_traj:
        with open(traj_file, 'r') as old_traj:
            for line in old_traj.readlines():
                new_traj.write(new_col + ' ' + line)

当然有更灵活和优雅的方法,但这应该适用于您的特定目录结构。

【讨论】:

  • 谢谢!让我立即检查并回复您
  • 你需要 Python3 并且脚本必须在 Data 所在的同一目录中启动,否则它将无法工作。
  • 目录部分没有说明,但我使用的是 python 2.7,我收到此错误Traceback (most recent call last): File "dict_name_file.py", line 15, in <module> if len(new_col) != 3 or not new_col.isnumeric() or not '/Trajectory/' in traj_file: AttributeError: 'str' object has no attribute 'isnumeric'
  • new_col = traj_file.split('/')[1] 更改为 new_col = unicode(traj_file.split('/')[1]) 或完全删除文件名检查
  • 当然,您可以根据需要更改输出格式,只需将“write”调用中的空格替换为制表符,并将“new_traj”的文件名替换为您需要的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2012-01-30
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多