3

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.

clemens
  • 16,716
  • 11
  • 50
  • 65
MatejMecka
  • 1,448
  • 2
  • 24
  • 37
  • 2
    better read from one file and write to another. When you write more data than it was before in line then it will overwrite next line. It will not resize line to keep more data. – furas Dec 09 '17 at 17:41

1 Answers1

3

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")
PythEch
  • 932
  • 2
  • 9
  • 21
  • Thanks for the answer but when I run it like 50 multiple copies of the same line happen. Also how do i keep the numbers and time durations? – MatejMecka Dec 09 '17 at 18:06
  • @UnknownDeveloper I'm having the difficulty imagining why, please see the updated post. – PythEch Dec 09 '17 at 18:21
  • The content is written to the file. Is there a way to also copy `5 00:00:12,013 --> 00:00:14,046` too? – MatejMecka Dec 09 '17 at 18:35
  • @UnknownDeveloper yes, just call `fileresp.write("%d %s --> %s\n%s" % (srt.index, srt.start, srt.end, str.content))`. The data you're looking for is available in the `srt` class, in this case you only need `start` and `end` additionally. – PythEch Dec 09 '17 at 18:40
  • 1
    Thanks a lot @PythEch! I have no words to express my thank you's to you – MatejMecka Dec 09 '17 at 18:41