Im trying to replace a file with the file translation' with python


#1

hello,

i’m trying to do something with python and im not sure here of the best way to do it.

i have a file, something like that :
“sentence 1 : the sentence is in english
sentence 2 : the sentence is also in english”

and i would like from an other file (its the translation of the first file) :
“sentence 1 : the sentence is in english / phrase 1 : la phrase est en francais
sentence 2 : the sentence is also in english/ phrase 2 : la phrase est aussi en francais”

at this end i would like to transform the file 1 in something like that :
phrase 1 : la phrase est en francais
phrase 2 : la phrase est aussi en francais"

so basically i have an english file (file 1) and a translation of this file (file 2) and i want to get a file with only the translation.
to write on the file directly im using that :

Read in the file

with open(‘URL FILE’) as file :
filedata = file.read()

with open(‘URL FILE’, ‘w’) as file:
file.write(filedata)

file.close()

and i was thinking about something like that :
if “sentence 1 : the sentence is in english” in filedata:
filedata = filedata.replace(‘sentence 1 : the sentence is in english’, ''sentence 1 : the sentence is in english / phrase 1 : la phrase est en francais")

but the thing is that i dont want to have to write the sentence. i would like that if the sentences of the file 1 is == to the sentence of the file 2, then it replace it. and then it delete the similar sentence (to remove the english part);;;

thanks,


#2

Salut :blush:

sentence 1 : the sentence is in english / phrase 1 : la phrase est en francais

J’ai un idée -
Tu peux utiliser :

.split('/')

"english / francais".split('/')
>>> ['english ', ' francais']
"sentence 1 : the sentence is in english / phrase 1 : la phrase est en francais".split('/')
>>> ['sentence 1 : the sentence is in english ', ' phrase 1 : la phrase est en francais']