简体   繁体   English

如何读取多个txt文件-Python

[英]How to read multiple txt files - Python

I'm new with python and need to read these files from a directory.我是 python 新手,需要从目录中读取这些文件。

MHBK_Trade_20210924_8387_20211007113214.txt
MHBK_TradeCash_20210924_8387_20211007113214.txt
MHEU_Trade_20210924_6144_20211007113525.txt
MHEU_TradeCash_20210924_6144_20211007113525.tx
Trade_20210924_7190_20211007113353.txt
TradeCash_20210924_7190_20211007113353.txt

I'm doing this way to read the files.我这样做是为了读取文件。

But this way every time I have to inform the complete name of the file.但是这样每次我都要告知文件的完整名称。 I would like to read only the first part, because this never changes.我只想阅读第一部分,因为这永远不会改变。

def findmefile(directory, containsInFilename):
    entity_filenames = {}
    for file in os.listdir(directory):
        if containsInFilename in file:
            entity_filenames[re.findall("(.*?)_", file)[0]] = file
    return entity_filenames


# Get the core Murex file names
MHItradefiles = findmefile(CoreMurexFilesLoc, "Trade_20210924_7190_20211007113353.txt")
# MHItradeCashfiles = findmefile("TradeCash_", CoreMurexFilesLoc)
# MHEUtradefiles = findmefile("MHEU_Trade_", CoreMurexFilesLoc)
# MHEUtradeCashfiles = findmefile("MHEU_TradeCash_", CoreMurexFilesLoc)

MHItradefiles = pd.read_csv(
    CoreMurexFilesLoc + "\\" + "Trade_20210924_7190_20211007113353.txt", delimiter="|"
)

print(MHItradefiles)

I would like to know if it is possible to read these files from the first part as they are from different entities.我想知道是否可以从第一部分读取这些文件,因为它们来自不同的实体。 Is this possible?这可能吗?

I need the values of a specific column, can I get the value by column name or by its index.我需要特定列的值,我可以按列名或其索引获取值吗? The txt file is delimited by | txt 文件由 | 分隔。 ? ?

I suggest first making a list of filenames in the folder.我建议首先在文件夹中列出文件名。

from glob import glob
import os

files = glob(CoreMurexFilesLoc + '\\*')

Then you can check wether an individual file exists in the folder: CoreMurexFilesLoc + "\\\\Trade_20210924_7190_20211007113353.txt" in files然后您可以检查文件夹中是否存在单个文件: CoreMurexFilesLoc + "\\\\Trade_20210924_7190_20211007113353.txt" in files

Or you can retrieve all filenames that start with a certain string:或者您可以检索以某个字符串开头的所有文件名:

MHItradeCashfiles = [i for i in files if os.path.basename(i).startswith('TradeCash_')] # or [i for i in files if 'TradeCash_' in i]

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

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