поиск по строкам и склейка id:hash и id:hash:pass или id:pass

Discussion in 'PHP' started by grad85, 16 Jul 2013.

  1. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0
    поиск по строкам и склейка id:hash и hash:pass в id:pass

    есть два файла
    1 - id:hash
    2 - hash:pass

    нужно получить третий файл формата

    id:hash:pass или id:pass

    то есть сравнить строки в двух файлах и строки у которых первые 32 символа совпадают записать в виде
    id:hash:pass или id:pass в третий файл.

    может у кого есть решение готовое? на php да или пофиг на чем. очень надо помогите.
     
    #1 grad85, 16 Jul 2013
    Last edited: 16 Jul 2013
  2. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    Вот 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'))
    
     
    1 person likes this.
  3. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0
    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

    ошибка какаято
     
  4. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    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'))
    
     
  5. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0
    Traceback (most recent call last):
    File "sx.py", line 28, in <module>
    id_, hash_ = l.split(':')
    ValueError: need more than 1 value to unpack

    я вообще лошара чтото у меня не получается запустить .
     
    #5 grad85, 16 Jul 2013
    Last edited: 16 Jul 2013
  6. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    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'))
    
     
  7. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0
    теперь скрипт выполняется без ошибок, но файл out.txt создается пустой.
     
  8. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    Значит я неправильно понял что в первом посту примеры файлов скинь.
     
  9. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0

    файл - 1.txt
    [email protected]:1111111111111111111111111111aa32
    [email protected]:1111111111111111111111111111aa33
    [email protected]:1111111111111111111111111111aa34
    [email protected]:1111111111111111111111111111aa35
    [email protected]:1111111111111111111111111111aa36
    [email protected]:1111111111111111111111111111aa37

    файл 2.txt
    1111111111111111111111111111aa32:password
    1111111111111111111111111111aa33:password1
    1111111111111111111111111111aa36:password2
    1111111111111111111111111111aa37:password3

    файл out.txt
    [email protected]:1111111111111111111111111111aa32:password

    или

    [email protected]:password

    без разницы.
    т.е. в файле 1.txt больше строк чем в 2.txt
    хз если какуето роль играет....
     
    #9 grad85, 16 Jul 2013
    Last edited: 16 Jul 2013
  10. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    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'))
    
     
  11. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0
    Traceback (most recent call last):
    File "ss.py", line 28, in <module>
    id_, hash_ = l.split(':')
    ValueError: need more than 1 value to unpack
     
  12. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    Из последнего поста качни ты что-то не то качнул.ю
     
  13. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0
    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
     
    #13 grad85, 16 Jul 2013
    Last edited: 16 Jul 2013
  14. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    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'))
     
  15. grad85

    grad85 New Member

    Joined:
    20 Jan 2012
    Messages:
    12
    Likes Received:
    0
    Reputations:
    0
    Спасибо. проси что хочешь). Пол дня искал везде.
     
Loading...
Similar Threads - поиск строкам склейка
  1. GAiN
    Replies:
    3
    Views:
    7,572