Using Python to process txt files

Source: Internet
Author: User
Tags python script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.