J
J
Jlokys2020-05-19 20:05:41
Python
Jlokys, 2020-05-19 20:05:41

Python doesn't overwrite changes to the file as it should, why?

The problem is that the program removes spaces and dashes from the file and then writes the file and the for loop is not executed in this file, why? How to fix it? But it is interesting that on the command line the whole process is displayed correctly, it is only written for some reason not so (
here is the code

def change():
    file_name = fd.askopenfilename()
    file_name1 = 'второй файл'
    f = open(file_name,'r')
    lines = f.read()
    lines = lines.replace('Путей в парке', '')
    lines = lines.replace(';--------------', '')
    while "  " in lines:
        lines = lines.replace("  ", " ")
    level1 = None
    level2 = None
    for line in lines.split('\n'):
        result1 = re.match('^@\s+(\d+)\s+(.*)$', line)
        if result1:
            level1 = f'{result1.group(1)};{result1.group(2)}'
            continue
        if re.match('#', line):
            level2 = line
            continue
        if level1 and level2:
            if level1 and level2 and line:
                line = lines.replace(' ', '; ')
                print(f'{level1};{level2}{line}')
                save_changes = open(file_name1, 'w')
                save_changes.writelines(line)
                save_changes.close()


Here is an example of a file
;--------------
@ 96140 Irradiation
# 01 Paths in the park: 2
1 ch 0 - - ec % 0000 r 1150 80 ?/? GL
2 hl 0 - - ec % 0000 r 1150 80 ?/? CH
# 02 Paths in the park: 10
3 to 0 - - ec % 0000 r 1150 80 ?/? ON
# 03 Paths in the park: 11
20 to 0 - - ets % 0000 r 1150 80 ?/? ON
;--------------
@ 91000 Tynda
# 01 Ways in the park: 4
1 to 0 - - ets % 0000 r 1150 80 ?/? Software

Should be like this

64759;Akkul;# 01 Ways in the park: 4; one; ch; 0; -; -; ets; -; 6630; R; 886/870; 60/59; +/-; WAY; GL1;
64759;Akkul;# 01 Paths in the park: 4; 2; ch; 0; -; -; ets; -; 6630; R; 861; 58; +/-; GL;
64759;Akkul;# 01 Paths in the park: 4; 3; on; 0; -; -; ets; -; 6630; R; 899; 61; +/-; ON;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question