打开多个文本文件和调用函数

时间:2017-06-12 20:41:33

标签: python

下面的代码工作正常,它打开一个文本文件,函数parse_messages作为参数

def parse_messages(hl7):
    hl7_msgs = hl7.split("MSH|")
    hl7_msgs = ["{}{}".format("MSH|", x) for x in hl7_msgs if x]
    for hl7_msg in hl7_msgs:
       #does something..

with open('sample.txt', 'r') as f:
    hl7 = f.read()
df = parse_messages(hl7)

但是现在我在目录中有多个文本文件。我想打开每一个然后从parse_messages函数调用。这是我到目前为止所尝试的内容。

但是这只读取了最后一个文本文件,而不是所有文本

import glob
data_directory = "C:/Users/.../"
hl7_file = glob.glob(data_directory + '*.txt')

for file in hl7_file:
    with open(file, 'r') as hl7:
    hl7 = f.read()
df = parse_messages(hl7)

2 个答案:

答案 0 :(得分:1)

在您的读取文件循环for file in hl7_file中,您在每次迭代时都会覆盖hl7,只留下hl7处的最后一个读取存储 您可能想要将文件的所有内容连接在一起

hl7 = ''
for file in hl7_file:
    with open(file, 'r') as f:
        hl7 += f.read()

df = parse_messages(hl7) # process all concatenate contents together

或者你可以在循环内调用parse_messages函数,df list存储结果如下

df = []
for file in hl7_file:
    with open(file, 'r') as f:
        hl7 = f.read()
        df.append(parse_messages(hl7))
# df[0] holds the result for 1st file read, df[1] for 2nd file and so on

答案 1 :(得分:0)

如果我明白你想做什么,这应该有用

import os

all = []
files = [x for x in os.listdir() if x.endswith(".txt")]
for x in files:
    with open(x, encoding='utf-8','r') as fileobj:
        content = fileobj.read()
        all.append(parse_message(content))