【发布时间】: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