【问题标题】:Filtering by values in text list in Python在 Python 中按文本列表中的值过滤
【发布时间】:2017-04-24 10:31:43
【问题描述】:

我编写了一个 Python 脚本,试图过滤一些文本文件并将它们与另一个文本文件进行比较,但是我正在努力寻找解决方案。

below | 1

above | 2

above | 3

above | 4

above | 5

below | 6

below | 7

below | 8

below | 9

below | 10

below | 11

below | 12

above | 13

below | 14

below | 15

below | 16

below | 17

below | 18

below | 19

below | 20

below | 21

...

我有这个文件列出了视频帧以及它们是否高于或低于定义的阈值。

此外,我有一个“高于”阈值帧的列表,以及每个标记的用户定义值(x、y 或 z)。不幸的是,此列表中的数字与初始的上下帧编号不对应,而只是一个编号列表。

y | 1
x | 2
x | 3
y | 4
z | 5
z | 6
y | 7
z | 8
y | 9
y | 10
x | 11
x | 12
y | 13
x | 14
x | 15
x | 16
x | 17
x | 18
y | 19
x | 20
z | 21

我想将这两者结合起来,以使上述帧的 x、y 或 z 值替换另一个脚本中的 'above' 标记,如下所示:

below | 1

y | 2

x | 3

x | 4

y | 5

below | 6

below | 7

below | 8

below | 9

below | 10

below | 11

below | 12

z | 13

below | 14

below | 15

below | 16

below | 17

below | 18

below | 19

below | 20

below | 21

但是我不知道如何遍历列表来实现这一点。我应该将值存储在字典中并对其进行迭代吗?任何帮助将非常感激。我尝试过使用一些 for 循环和 open 语句来尝试它,但我无法理解如何迭代它:

 with open((selectedvideostring + 'combinedfiles.txt'), 'w') as combinedfile:
    with open((selectedvideostring + 'aboveorbelow.txt'), 'r') as aboveorbelowfile:
        for line in aboveorbelowfile:
            if 'above' in line:
                with open((selectedvideostring + 'xyzfile.txt'), 'r') as xyzfile:
                    for line in xyzfile:
                        if 'x' in line:
                            combinedfile.write("x" + '|' + str(int(cap.get(1))))

                        elif 'y' in line:
                            combinedfile.write("y" + '|' + str(int(cap.get(1))))

                        if 'z' in line:
                            combinedfile.write("z" + '|' + str(int(cap.get(1))))

            elif 'below' in line:
                combinedfile.write("below" + '|' + str(int(cap.get(1))))

谢谢!

【问题讨论】:

  • 你已经尝试了什么?
  • 请将这些文件的内容粘贴到您的问题中,这样我们就不必重新输入所有内容来测试可能的解决方案。
  • 现在就这么做,抱歉!
  • 对,粘贴内容,还有我目前的垃圾代码!

标签: python list python-3.x loops text


【解决方案1】:

您应该同时打开这两个文件,而不是在外部上方或下方文件循环内打开和迭代 xyz 文件。文件是迭代器,因此您可以使用for 循环来迭代上方或下方文件中的行,并使用next 在遇到“上方”​​条目时从 xyz 文件中获取下一行。

with open("aboveorbelow.txt") as aob, open("xyzfile.txt") as xyz:
    for line in aob:
        if line.startswith("above"):
            ab, c = line.split(" | ")
            d, _ = next(xyz).split(" | ")
            print(" | ".join((d, c)))
        elif line.startswith("below"):
            print(line)

(使用print 是为了简化测试,当然对于输出文件也是如此。)

【讨论】:

  • 感谢您的帮助!一个小问题,在我的程序中实现该代码时出现错误,读取“StopIteration”,将错误抛出d, _ = next(xyz).split(" | ")
  • @KittenMittons 在这种情况下,您的第二个文件中没有足够的 xyz 值。如果存在可用的默认值,您可以使用带有默认值的next,例如next(xyz, "UNKNOWN | 0").split(" | ").
  • 非常感谢 - 这已经停止了 StopIteration 问题 - 当我使用上面的那些打印语句时,它会完全按照我想要的方式输出,但是当我尝试将这些行写入新的输出文件时,它完成没有错误,但不存在文件 - 我现在将添加我正在使用的代码作为原始问题中的编辑,我一定做错了
  • 没关系,是我没有保存正确的文件路径——我已经整理好了,非常感谢你的帮助!!
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 2021-09-17
相关资源
最近更新 更多