Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打开多个文本文件并调用函数_Python - Fatal编程技术网

Python 打开多个文本文件并调用函数

Python 打开多个文本文件并调用函数,python,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')

下面的代码工作得很好,它打开一个文本文件,函数
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)

在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

如果我明白你想做什么,这应该行得通

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))

正确缩进代码。它在某些地方有语法错误,在其他地方没有意义。Python是空间敏感的。您的
hl7
从文件中读出的内容在每次迭代时都会被覆盖,只留下最后一次读取的文件,您可能希望将它们附加到列表或string@MadPhysicist我刚刚编辑过。它来自复制粘贴当然它只读取最后一个文本文件,您将每个文件存储在hl7中,然后用下一个文件覆盖它。一旦您覆盖了所有这些消息,您就可以调用“解析消息”。@Skycc我会将它们附加到新列表中,但如何从列表中打开每个消息?这非常有效,谢谢!我实际上看起来更像是第二个答案:)谢谢!检查你的parse_messages函数,它应该会返回你期望的结果,不管是最后的字符串还是列表,看起来你正在运行
df.append(parse_messages)
而不是
df.append(parse_messages(hl7))
当你遇到错误时,它工作了,而不是复制我正在键入的内容,我想我没有键入所有内容:)