【问题标题】:Loading data from text file from only a part of the file name仅从文件名的一部分从文本文件加载数据
【发布时间】:2016-10-06 08:21:57
【问题描述】:

我在不同的文本文件中有很多数据。每个文件名都包含一个我选择的单词,但它们也包含很多“乱码”。例如,我有一个名为 datapoints-(my chosen name)-12iu8w9e8v09wr-140-ad92-dw9

的文本文件

所以datapoints 字符串在所有文本文件中,(my chosen name) 是我定义的并且知道如何在我的代码中提取,但最后一位是随机的。而且我不想去删除我拥有的每个文本文件中的那个部分,这会有点耗时。

我只想加载这些文本文件,但我不确定如何在不使用“乱码”的情况下定位每个文件。我只想说:“加载包含(我选择的名称)的文件”,然后不用担心其余部分。

【问题讨论】:

标签: python


【解决方案1】:

这会返回使用glob module的所有文件的列表

import glob
your_words = ['word1', 'word2']
files = []
# find files matching 'datapoint-your words-*.txt'
for word in your_words:
    # The * is a wildcard, your words are filled in the {}. one by one
    files.extend(glob.glob('datapoint-{}-*.txt'.format(word)))

print files

【讨论】:

    猜你喜欢
    • 2020-01-27
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多