【问题标题】:Python 3 pandas directory search for a string in filenamePython 3 pandas 目录在文件名中搜索字符串
【发布时间】:2017-02-11 17:55:09
【问题描述】:

您好 StackExchange!

尝试打印目录中的所有文件,但这次我只想打印文件名中包含字符串 ..."AMX_error"...csv 的所有 .csv 文件。我的“所有 .csv”都在工作,但缺少一些搜索逻辑。

import glob
import pandas as pd

path = r'C:\Users\Desktop\Experiment\'

#Following command to search for string in the filename
allFiles = glob.glob(path + "/*.csv") & (search filename 'AMX_error' = true)

for filename in allFiles:
    print(filename)

#rest of code..

在文件名中搜索字符串的表示法是什么?谢谢!

【问题讨论】:

    标签: csv pandas import filenames python-3.5


    【解决方案1】:

    除非您有理由首先过滤文件,否则您可以在 for 循环中检查感兴趣的字符串是否在文件名中。

    import glob
    import pandas as pd
    
    path = r'C:\Users\Desktop\Experiment'
    
    #Following command to search for string in the filename
    allFiles = glob.glob(path + "/*.csv")
    
    for filename in allFiles:
        if 'AMX_error' in filename:
            print(filename)
    

    【讨论】:

    • 从未想过使用 if-then 循环来识别特定文件名。这样可行!应该能够处理我的细微差别(按文件名)数据集。
    猜你喜欢
    • 1970-01-01
    • 2015-12-25
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多