поиск по строкам и склейка id:hash и hashass в idass есть два файла 1 - id:hash 2 - hashass нужно получить третий файл формата id:hashass или idass то есть сравнить строки в двух файлах и строки у которых первые 32 символа совпадают записать в виде id:hashass или idass в третий файл. может у кого есть решение готовое? на php да или пофиг на чем. очень надо помогите.
Вот python скрипт. Code: #!/usr/bin/env python #-*- coding: utf-8 -*- from sys import exit ID_HASH_PATH = '1.txt' HASH_PASS_PATH = '2.txt' OUT_PATH = 'out.txt' def open_file(fn, mode='rt'): try: if mode == 'rt': return [l.rstrip() for l in open(fn, mode).readlines()] else: return open(fn, mode, 0) except IOError as e: print "Can't open file: %s error: %s" % (fn, e.strerror) exit() fist = open_file(ID_HASH_PATH) two = open_file(HASH_PASS_PATH) out = open_file(OUT_PATH, 'wt') t_map = dict([i.split(':', 1) for i in two]) for l in fist: id_, hash_ = l.split(':') if id_ in t_map: out.write('%s:%s:%s%s' % (id_, hash_, t_map[id_], '\n'))
Traceback (most recent call last): File "sc.py", line 25, in <module> t_map = dict([i.split(':', 1) for i in two]) ValueError: dictionary update sequence element #4772 has length 1; 2 is required ошибка какаято
Code: #!/usr/bin/env python #-*- coding: utf-8 -*- from sys import exit ID_HASH_PATH = '1.txt' HASH_PASS_PATH = '2.txt' OUT_PATH = 'out.txt' def open_file(fn, mode='rt'): try: if mode == 'rt': return [l.rstrip() for l in open(fn, mode).readlines()] else: return open(fn, mode, 0) except IOError as e: print "Can't open file: %s error: %s" % (fn, e.strerror) exit() fist = open_file(ID_HASH_PATH) two = open_file(HASH_PASS_PATH) out = open_file(OUT_PATH, 'wt') t_map = dict([i.split(':', 1) for i in two if ':' in i]) for l in fist: id_, hash_ = l.split(':') if id_ in t_map: out.write('%s:%s:%s%s' % (id_, hash_, t_map[id_], '\n'))
Traceback (most recent call last): File "sx.py", line 28, in <module> id_, hash_ = l.split(':') ValueError: need more than 1 value to unpack я вообще лошара чтото у меня не получается запустить .
Code: #!/usr/bin/env python #-*- coding: utf-8 -*- from sys import exit ID_HASH_PATH = '1.txt' HASH_PASS_PATH = '2.txt' OUT_PATH = 'out.txt' def open_file(fn, mode='rt'): try: if mode == 'rt': return [l.rstrip() for l in open(fn, mode).readlines()] else: return open(fn, mode, 0) except IOError as e: print "Can't open file: %s error: %s" % (fn, e.strerror) exit() fist = open_file(ID_HASH_PATH) two = open_file(HASH_PASS_PATH) out = open_file(OUT_PATH, 'wt') t_map = dict([i.split(':', 1) for i in two if ':' in i]) for l in fist: if ':' in l: id_, hash_ = l.split(':', 1) if id_ in t_map: out.write('%s:%s:%s%s' % (id_, hash_, t_map[id_], '\n'))
файл - 1.txt [email protected]:1111111111111111111111111111aa32 [email protected]:1111111111111111111111111111aa33 [email protected]:1111111111111111111111111111aa34 [email protected]:1111111111111111111111111111aa35 [email protected]:1111111111111111111111111111aa36 [email protected]:1111111111111111111111111111aa37 файл 2.txt 1111111111111111111111111111aa32assword 1111111111111111111111111111aa33assword1 1111111111111111111111111111aa36assword2 1111111111111111111111111111aa37assword3 файл out.txt [email protected]:1111111111111111111111111111aa32assword или [email protected]assword без разницы. т.е. в файле 1.txt больше строк чем в 2.txt хз если какуето роль играет....
Code: #!/usr/bin/env python #-*- coding: utf-8 -*- from sys import exit ID_HASH_PATH = '1.txt' HASH_PASS_PATH = '2.txt' OUT_PATH = 'out.txt' def open_file(fn, mode='rt'): try: if mode == 'rt': return [l.rstrip() for l in open(fn, mode).readlines()] else: return open(fn, mode, 0) except IOError as e: print "Can't open file: %s error: %s" % (fn, e.strerror) exit() fist = open_file(ID_HASH_PATH) two = open_file(HASH_PASS_PATH) out = open_file(OUT_PATH, 'wt') t_map = dict([i.split(':', 1) for i in two if ':' in i]) for l in fist: if ':' in l: id_, hash_ = l.split(':', 1) if hash_ in t_map: out.write('%s:%s:%s%s' % (id_, hash_, t_map[id_], '\n'))
Traceback (most recent call last): File "ss.py", line 28, in <module> id_, hash_ = l.split(':') ValueError: need more than 1 value to unpack
Traceback (most recent call last): File "sa.py", line 31, in <module> out.write('%s:%s:%s%s' % (id_, hash_, t_map[id_], '\n')) KeyError: '[email protected]' эта почта - это сторка номер один из списка id:hash
Code: #!/usr/bin/env python #-*- coding: utf-8 -*- from sys import exit ID_HASH_PATH = '1.txt' HASH_PASS_PATH = '2.txt' OUT_PATH = 'out.txt' def open_file(fn, mode='rt'): try: if mode == 'rt': return [l.rstrip() for l in open(fn, mode).readlines()] else: return open(fn, mode, 0) except IOError as e: print "Can't open file: %s error: %s" % (fn, e.strerror) exit() fist = open_file(ID_HASH_PATH) two = open_file(HASH_PASS_PATH) out = open_file(OUT_PATH, 'wt') t_map = dict([i.split(':', 1) for i in two if ':' in i]) for l in fist: if ':' in l: id_, hash_ = l.split(':', 1) if hash_ in t_map: out.write('%s:%s:%s%s' % (id_, hash_, t_map[hash_], '\n'))