Search code examples
pythonpython-3.xdeepl

Python: Having trouble replacing lines from file


I'm trying to build a translator using deepl for subtitles but it isn't running perfectly. I managed to translate the subtitles and most of the part I'm having problems replacing the lines. I can see that the lines are translated because it prints them but it doesn't replace them. Whenever I run the program it is the same as the original file.

This is the code responsible for:

def translate(input, output, languagef, languaget):
    file = open(input, 'r').read()
    fileresp = open(output,'r+')
    subs = list(srt.parse(file))
    for sub in subs:
        try:
            linefromsub = sub.content
            translationSentence = pydeepl.translate(linefromsub, languaget.upper(), languagef.upper())
            print(str(sub.index) + ' ' + translationSentence)
            for line in fileresp.readlines():
                newline = fileresp.write(line.replace(linefromsub,translationSentence))
        except IndexError:
            print("Error parsing data from deepl")

This is the how the file looks:

1
00:00:02,470 --> 00:00:04,570
        - Yes, I do.
        - (laughs)

2
00:00:04,605 --> 00:00:07,906
      My mom doesn't want
      to babysit everyday

3
00:00:07,942 --> 00:00:09,274
        or any day.

4
00:00:09,310 --> 00:00:11,977
        But I need
    my mom's help sometimes.

5
00:00:12,013 --> 00:00:14,046
        She's just gonna
    have to be grandma today. 

Help will be appreaciated :) Thanks.


Solution

  • You are opening fileresp with r+ mode. When you call readlines(), the file's position will be set to the end of the file. Subsequent calls to write() will then append to the file. If you want to overwrite the original contents as opposed to append, you should try this instead:

    allLines = fileresp.readlines()
    fileresp.seek(0)    # Set position to the beginning
    fileresp.truncate() # Delete the contents
    for line in allLines:
        fileresp.write(...)
    

    Update

    It's difficult to see what you're trying to accomplish with r+ mode here but it seems you have two separate input and output files. If that's the case consider:

    def translate(input, output, languagef, languaget):
        file = open(input, 'r').read()
        fileresp = open(output, 'w') # Use w mode instead
        subs = list(srt.parse(file))
        for sub in subs:
            try:
                linefromsub = sub.content
                translationSentence = pydeepl.translate(linefromsub, languaget.upper(), languagef.upper())
                print(str(sub.index) + ' ' + translationSentence)
                fileresp.write(translationSentence) # Write the translated sentence
            except IndexError:
                print("Error parsing data from deepl")