Python:替换文件中的行时遇到问题
Python: Having trouble replacing lines from file
我正在尝试使用 deepl 构建翻译器来翻译字幕,但它 运行 并不完美。我设法翻译了字幕,但我在替换台词时遇到了大部分问题。我可以看到这些行已被翻译,因为它打印了它们但没有替换它们。每当我 运行 程序时,它与原始文件相同。
这是负责的代码:
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")
这是文件的样子:
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.
我们将不胜感激 :)
谢谢
您正在以 r+
模式打开 fileresp
。当您调用 readlines()
时,文件的位置将被设置为文件的末尾。随后对 write()
的调用将追加到文件中。如果你想覆盖原始内容而不是追加,你应该试试这个:
allLines = fileresp.readlines()
fileresp.seek(0) # Set position to the beginning
fileresp.truncate() # Delete the contents
for line in allLines:
fileresp.write(...)
更新
很难在此处看到您尝试使用 r+
模式完成什么,但您似乎有两个独立的输入和输出文件。如果是这种情况,请考虑:
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")
我正在尝试使用 deepl 构建翻译器来翻译字幕,但它 运行 并不完美。我设法翻译了字幕,但我在替换台词时遇到了大部分问题。我可以看到这些行已被翻译,因为它打印了它们但没有替换它们。每当我 运行 程序时,它与原始文件相同。
这是负责的代码:
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")
这是文件的样子:
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.
我们将不胜感激 :) 谢谢
您正在以 r+
模式打开 fileresp
。当您调用 readlines()
时,文件的位置将被设置为文件的末尾。随后对 write()
的调用将追加到文件中。如果你想覆盖原始内容而不是追加,你应该试试这个:
allLines = fileresp.readlines()
fileresp.seek(0) # Set position to the beginning
fileresp.truncate() # Delete the contents
for line in allLines:
fileresp.write(...)
更新
很难在此处看到您尝试使用 r+
模式完成什么,但您似乎有两个独立的输入和输出文件。如果是这种情况,请考虑:
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")