Answer the question
In order to leave comments, you need to log in
How to write text to yaml file in python?
I am writing an automatic translation for the localization of a game. The problem is that the game only accepts files with the YAML extension. I solved this problem by simply creating a textbook, and then transcoding it to .yml and it even worked ... For Unix systems. Windows creates .yml.tmp files and I obviously made a crutch when transcoding. Help me fix my crutch please. Here is my code
Function to write to file
def execute_operation(self, path_general_file, path_additional_file):
list_english_line = self.file_in_list(path_general_file)
dict_english_line = self.file_in_dict(path_general_file)
additional_file = self.write_in_file(path_additional_file)
for line in list_english_line:
if 'l_english' in line:
additional_file.write('l_russian:\n')
elif '#' in line:
additional_file.write(line)
elif '\n' in line:
additional_file.write('\n')
elif line == ' \n':
additional_file.write(' \n')
elif line == ' \n':
additional_file.write(' \n')
else:
try:
#self.__check_connection()
ru_text = self.translator.transleate_text(text=dict_english_line[line].replace(':0 "', '').replace('"', ''))
ru_text = ':0 "' + ru_text + '"'
additional_file.write(line + ru_text + '\n')
if 'desc' in line:
additional_file.write('\n')
except:
additional_file.write(line)
additional_file.close()
print('Файл' + ' ' + path_additional_file.split('/')[-1] + ' Переведён')
self.encod_utf8_bom(path_additional_file)
def encod_utf8_bom(self, path_on_file: str):
with codecs.open(path_on_file, encoding="utf-8") as f_in, codecs.open(path_on_file + ".tmp", encoding="utf-8-sig", mode="w") as f_out:
f_out.write(f_in.read())
shutil.move(path_on_file + ".tmp", path_on_file)
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