Some time ago to do a company an automatic translation project needs to deal with a large number of copy fields, manual to do is not likely (lazy), so borrowed Python script automated processing off, here to record.
Import linecachedef outputfile (i,j,n): # zh = File_zh.read (). Decode (' Utf-8 '). Encode (' GBK ', ' ignore ') file_new = open (' 1. TXT ', ' r+ ') for L in range (i,j+1): # line = Linecache.getline (' Tw.txt ', L). Decode (' Utf-8 '). Encode (' GBK ', ' ignore ') Line1 = s TR (linecache.getline (' Zh.txt ', L) [0:-1]). Strip () line2 = str (linecache.getline (' Tw.txt ', l+n) [0:-1]). Strip () if (Len ( STR (line1[0:-1])) ==0): Continuefile_new.write (' MsgId "' + line1 + ' \ n ') file_new.write (' msgstr ' + line2 + '" \ n ') File_new.close () outputfile (1,25,3)
Build Effect:
# These are the test fields MsgId "Hello world!" Msgstr "World Hello!" MsgId "Python is a good Language." Msgstr "Python is a good language." MsgId " China" msgstr "msgid" test Ah "MSGSTR" Testing it!!! " MsgId "Hello% (useraaa) s!" Msgstr "Hello% (useraaa) s!"
The code is very simple, the procedure is to read a line of characters from two different files, and then output to a new file in a line by a certain format, you need to note that TXT file needs to be saved as UTF-8 encoding format, otherwise transcoding, a little trouble ~
Using Python to process txt files