比较 .txt 文件中的 .jpg 文件并合并数据
Compare .jpg files inside of a .txt file and merge data
从a.txt-
/set03/V001/visible/I00875.jpg 333,212,354,254,0
/set03/V001/visible/I00955.jpg 469,224,524,348,0
/set03/V001/visible/I00955.jpg 392,212,424,276,0
/set03/V001/visible/I00773.jpg 343,218,369,263,0
/set03/V001/visible/I00773.jpg 357,216,381,264,0
/set03/V001/visible/I00773.jpg 276,204,296,246,0
/set03/V001/visible/I01236.jpg 229,207,249,233,0
/set03/V001/visible/I00484.jpg 324,191,344,240,0
/set03/V001/visible/I00484.jpg 315,194,337,246,0
找到匹配的 .jpg 文件并将每个 .img 的颜色代码合并为一行,然后使用 Python-
放入一个新文件 b.txt
/set03/V001/visible/I00875.jpg 333,212,354,254,0
/set03/V001/visible/I00955.jpg 469,224,524,348,0 392,212,424,276,0
/set03/V001/visible/I00773.jpg 343,218,369,263,0 357,216,381,264,0 276,204,296,246,0
/set03/V001/visible/I01236.jpg 229,207,249,233,0
/set03/V001/visible/I00484.jpg 324,191,344,240,0 315,194,337,246,0
使用defaultdict set to list,得到结果。 Python 我使用的版本是 3.7 版本(它使项目的顺序与它们输入字典的顺序相同)。
from collections import defaultdict
d = defaultdict(list)
fin = open('f4.txt', 'r')
for line in fin:
file, color = line.split()
d[file].append(color)
for file, colors in d.items():
print(file, ' '.join(colors))
输出为:
/set03/V001/visible/I00875.jpg 333,212,354,254,0
/set03/V001/visible/I00955.jpg 469,224,524,348,0 392,212,424,276,0
/set03/V001/visible/I00773.jpg 343,218,369,263,0 357,216,381,264,0 276,204,296,246,0
/set03/V001/visible/I01236.jpg 229,207,249,233,0
/set03/V001/visible/I00484.jpg 324,191,344,240,0 315,194,337,246,0
编辑:要打印到输出文件,您需要打开:
fout = open('b.txt', 'w')
并且在 for 循环中:
fout.write(file + ' ' + ' '.join(colors) + '\n')
从a.txt-
/set03/V001/visible/I00875.jpg 333,212,354,254,0
/set03/V001/visible/I00955.jpg 469,224,524,348,0
/set03/V001/visible/I00955.jpg 392,212,424,276,0
/set03/V001/visible/I00773.jpg 343,218,369,263,0
/set03/V001/visible/I00773.jpg 357,216,381,264,0
/set03/V001/visible/I00773.jpg 276,204,296,246,0
/set03/V001/visible/I01236.jpg 229,207,249,233,0
/set03/V001/visible/I00484.jpg 324,191,344,240,0
/set03/V001/visible/I00484.jpg 315,194,337,246,0
找到匹配的 .jpg 文件并将每个 .img 的颜色代码合并为一行,然后使用 Python-
放入一个新文件 b.txt/set03/V001/visible/I00875.jpg 333,212,354,254,0
/set03/V001/visible/I00955.jpg 469,224,524,348,0 392,212,424,276,0
/set03/V001/visible/I00773.jpg 343,218,369,263,0 357,216,381,264,0 276,204,296,246,0
/set03/V001/visible/I01236.jpg 229,207,249,233,0
/set03/V001/visible/I00484.jpg 324,191,344,240,0 315,194,337,246,0
使用defaultdict set to list,得到结果。 Python 我使用的版本是 3.7 版本(它使项目的顺序与它们输入字典的顺序相同)。
from collections import defaultdict
d = defaultdict(list)
fin = open('f4.txt', 'r')
for line in fin:
file, color = line.split()
d[file].append(color)
for file, colors in d.items():
print(file, ' '.join(colors))
输出为:
/set03/V001/visible/I00875.jpg 333,212,354,254,0
/set03/V001/visible/I00955.jpg 469,224,524,348,0 392,212,424,276,0
/set03/V001/visible/I00773.jpg 343,218,369,263,0 357,216,381,264,0 276,204,296,246,0
/set03/V001/visible/I01236.jpg 229,207,249,233,0
/set03/V001/visible/I00484.jpg 324,191,344,240,0 315,194,337,246,0
编辑:要打印到输出文件,您需要打开:
fout = open('b.txt', 'w')
并且在 for 循环中:
fout.write(file + ' ' + ' '.join(colors) + '\n')