Answer the question
In order to leave comments, you need to log in
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()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question