【问题标题】:Reading two text file simultaneously in python 3.0 and extracting required string在 python 3.0 中同时读取两个文本文件并提取所需的字符串
【发布时间】:2015-01-16 15:42:36
【问题描述】:

我有两个文本文件,其数据类似于

文件_1:

data1 data_1 1
data2 data_2 2
data3 data_2 2 
data2 data_4 1
data3 data_3 1 and so on....

等等

文件_2:

data1
data2 
data1
data3
data2

我想得到一个输出为

data1:
      > data1 data_1 1
      > data1 data_3 2

data2:
      > data2 data_2 2
      > data2 data_4 1

data3:
      > data3 data_3 1

等等……

我从当前代码中得到的信息:

data1:
      > data1 data_1 1

data2:
      > data2 data_2 2

data3:
      > data3 data_2 2
      > data2 data_4 1
      > data3 data_3 1

代码:

first_occurance = {}
    with open("folder_1/file_1", "r") as file_1:
        with open("folder_1/file_2", "r") as file_2:
            for line_1,line_2 in zip(file_1, file_2):
                only_command = line_1.split()[0]
                if only_command in line_2:
                    if only_command not in first_occurance:
                        print ("\n   " + only_command + " :\n")
                        print ("      > " + line_1.strip())
                    else:
                        print ("      > " + line_1.strip())
                    first_occurance[only_command] = only_command

但这并没有按预期工作,因为数据没有根据标题格式化,例如data2 对应的行也显示在data3 中。对此问题的任何指导都会非常有帮助....

【问题讨论】:

  • 你能描述一下发生了什么吗?
  • 我编辑了这个问题。希望现在更清楚...
  • 不完全,仍然。那么您希望data3 会发生什么?应该打印在 data2 块下面吗?
  • 您的问题不是从多个文件中读取 - 这是您进行迭代的方式,但目前还不清楚您试图从描述中获得什么样的结果。
  • @user3467349 我现在已经编辑了它...希望它更好地理解....

标签: python python-3.x text


【解决方案1】:

以下是我认为您可能正在尝试做的事情:

from collections import defaultdict

data = """data1 data_1 1
data2 data_2 2
data1 data_3 2
data3 data_4 1
data2 data_3 1"""

commands = """data1
data2
data1
data3
data2"""

store = defaultdict(list)

for line, cmd in zip(data.split('\n'), commands.split('\n')):
    if line.startswith(cmd):
        store[cmd].append(line.strip())

for command in sorted(store):
    print("\n{}:".format(command))
    for l in store[command]:
        print("      >", l)

这会产生以下输出:

data1:
      > data1 data_1 1
      > data1 data_3 2

data2:
      > data2 data_2 2
      > data2 data_3 1

data3:
      > data3 data_4 1

对于命令中的每一行(您从file_2 读取的内容),如果数据中完全相同的行(file_1)以相同的“命令”开头,则将其存储。顺便说一句,您正在大量更改数据,我不确定我们是否了解您想要的内容。看来,file_2 甚至没用,或者您可能要重新对齐数据?

无论如何,存储分组数据后,您可以按排序顺序(data1、2、3...)打印组。您必须存储所有组,否则您将不得不为每个(数据)组一次又一次地读取文件。如果你没有得到你当前的输出 - 因为你在收到数据时打印数据。

但是,似乎根本不需要您的 file_2 数据,至少根据您在问题中想要的输出。所以这里是产生你想要的输出的文件阅读版本;注意它不需要读取file_2:

from collections import defaultdict

store = defaultdict(list)

with open("folder_1/file_1", "r") as data:
    for line in data:
        cmd, content = line.split(' ', 1)
        store[cmd].append(line.strip())

for cmd in sorted(store):
    print("\n{}:".format(cmd))
    for line in store[cmd]:
        print("      >", line)

【讨论】:

  • 我收到一个错误,因为 for line_1,line_2 in zip(file_1.split('\n'), file_2.split('\n')): AttributeError: '_io.TextIOWrapper '对象没有属性'split'
  • 因为您正在对文件句柄/流进行拆分。请检查更新,代码与您的文件读取版本一致。
  • 另外,请注意,我不明白你为什么还要检查file_2?您似乎正在更改数据的分配?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 2022-10-06
  • 2020-08-07
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多