比较.txt文件中的.jpg文件并合并数据

时间:2019-03-06 02:32:13

标签: python python-3.x python-2.x

来自a.txt-

/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

找到匹配的.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
/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

1 个答案:

答案 0 :(得分:1)

使用defaultdict设置为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')