如何在我的.po文件的空字段中添加文本
import re
from deep_translator import GoogleTranslator
fname = input('Enter file name: ')
if (len(fname) < 1): fname = 'text.po'
fh = open(fname, 'r+')
translated=list()
for line in fh:
if not line.startswith('msgid '):
continue
piece=re.findall('"(.*)"', line)
texte = GoogleTranslator(source='auto', target='french').translate(piece[0])
translated.append(texte)
print(translated)
for line in fh:
i =+ 0
if not line.startswith('msgstr '):
continue
piece2=re.findall('"()"', line)
for space in piece2:
fh.write(line.replace(space, translated[i] ))
fh.close()
msgid“某事在这里”
msgstr“在这里无”
提取“这里的东西”,我翻译并放回“这里”
目的
parse parse parse texte值
plass
- msgid“我joe”
- msgstr“”
所需的输出
- msgid“我joe”
- msgstr“ je suis joe”
替换函数不起作用。如何修复它
import re
from deep_translator import GoogleTranslator
fname = input('Enter file name: ')
if (len(fname) < 1): fname = 'text.po'
fh = open(fname, 'r+')
translated=list()
for line in fh:
if not line.startswith('msgid '):
continue
piece=re.findall('"(.*)"', line)
texte = GoogleTranslator(source='auto', target='french').translate(piece[0])
translated.append(texte)
print(translated)
for line in fh:
i =+ 0
if not line.startswith('msgstr '):
continue
piece2=re.findall('"()"', line)
for space in piece2:
fh.write(line.replace(space, translated[i] ))
fh.close()
msgid "Something goes here"
msgstr "nothing here"
extract "something goes here" which I translate and put back into the "nothing here"
purpose
parse texte value in piece2
text.po file
- msgid "i'm Joe"
- msgstr ""
desired output
- msgid "i'm Joe"
- msgstr "Je suis Joe"
replace function not working. how to fix it please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种方法可以做到这一点,使用输入文件“ text.txt”带有内容:
所需的输出为:
(选项1)单独的读取文件和写文件
这将输出内容中的内容 new 文件“ textout.txt”,而原始文件“ text.txt”保持完整。
如果您不需要
翻译
列表中的翻译行,则可以使用以下代码(相同输出):(选项2)替换同一文件中的文本
此将更新“ text.txt”中的内容,而原始文件则将其重命名为“ text.txt.bak”
There are 2 ways to do it, using the input file "text.txt" with contents:
And the desired output is:
(Option 1) separate read file and write file
This will output the contents in a new file "textout.txt", while the original file "text.txt" remains intact.
If you do not need the translated lines in the
translated
list, you could use below code (same output):(Option 2) replace text in the same file
This will update the contents in "text.txt", while the original file is renamed as "text.txt.bak"