我正在使用BeautifulSoup和googletrans软件包翻译Xliff文件。我设法提取所有字符串并翻译它们,并设法用翻译创建新标记来替换字符串,例如
<trans-unit id="100890::53706_004">
<source>Continue in store</source>
<target>Kontynuuj w sklepie</target>
</trans-unit>
当源标记中有其他标记时,问题就会出现
例如
<source><x ctype="x-htmltag" equiv-text="<b>" id="html_tag_191"/>Choose your product\
<x ctype="x-htmltag" equiv-text="</b>" id="html_tag_192"/>From a list: </source>
这些标记的数量不同,字符串出现的顺序也不同。例如<source> text1 <x /> <x/> text2 <x/> text3 </source>
。每个x标记都是唯一的,具有不同的id和属性
有没有一种方法可以在不创建新标记的情况下修改标记内的文本? 我想我可以提取x标记及其属性,但是不同代码行中的顺序或字符串和x标记有很大的不同,我不知道怎么做。 也许还有其他软件包更适合翻译xliff文件
要从
<source>
中提取两个文本条目,可以使用以下方法:给你:
我建议不要使用通用XML解析器解析XLIFF文件。相反,尝试寻找一个专门的XLIFF工具包。有一些python项目,但我没有使用它们的经验(我:主要是Java)
您可以使用
for
-loop处理source
中的所有子项。你可以用
copy.copy(child)
和append
到target
复制它们。同时,您可以检查
child
是否为NavigableString
并将其转换结果(为使其更具可读性,几乎没有重新格式化):
相关问题 更多 >
编程相关推荐