(kanji, a, b)
Swap stroke numbers in a kanjivg file
Swap stroke numbers in a kanjivg file
Swap stroke numbers in a kanjivg file
def swap_numbers(kanji, a, b):
    """Swap stroke numbers in a kanjivg file"""
    # We do hardly any checking. If something is wrong, just blow up.
    with open(kanji) as kf:
        lines = kf.readlines()
    num_a = -1
    num_b = -1
    line_a = ''
    line_b = ''
    line_a_pattern = number_text_pattern.format(a)
    line_b_pattern = number_text_pattern.format(b)
    for n, l in enumerate(lines):
        if line_a_pattern in l:
            num_a = n
            line_a = l
        if line_b_pattern in l:
            num_b = n
            line_b = l
    if num_a < 0 or num_b < 0:
        raise RuntimeError("Did not find both lines")
    lines[num_a] = line_b.replace(line_b_pattern, line_a_pattern)
    lines[num_b] = line_a.replace(line_a_pattern, line_b_pattern)
    with open(kanji, 'w') as kf:
        for l in lines:
            kf.write(l)
def swap_numbers ( kanji , a , b ) : # We do hardly any checking. If something is wrong, just blow up. with open ( kanji ) as kf : lines = kf . readlines ( ) num_a = - 1 num_b = - 1 line_a = '' line_b = '' line_a_pattern = number_text_pattern . format ( a ) line_b_pattern = number_text_pattern . format ( b ) for n , l in enumerate ( lines ) : if line_a_pattern in l : num_a = n line_a = l if line_b_pattern in l : num_b = n line_b = l if num_a < 0 or num_b < 0 : raise RuntimeError ( "Did not find both lines" ) lines [ num_a ] = line_b . replace ( line_b_pattern , line_a_pattern ) lines [ num_b ] = line_a . replace ( line_a_pattern , line_b_pattern ) with open ( kanji , 'w' ) as kf : for l in lines : kf . write ( l )