简体   繁体   English

从文件UTF-8编码utf-8 python到其他文件

[英]encoding utf-8 python from file UTF-8 to other file

I have two files in UTF-8, I need merge this files with a Python script, for each line in f1 (read by readlines() method), I do a writeline(l) in f2, but I need that f2 be UTF-8 file, How I can dou? 我在UTF-8中有两个文件,我需要将这些文件与Python脚本合并,对于f1中的每一行(通过readlines()方法读取),我在f2中执行writeline(l),但我需要f2为UTF -8文件,我怎么能dou?

Thanks 谢谢

You can use the open method from the codecs module (instead of open(file,'w') ): 您可以使用编解码器模块中的open方法(而不是open(file,'w') ):

import codecs

fileNames = ['file1.txt', 'file2.txt']

with codecs.open('file3.txt', 'w', 'utf-8') as outfile:
    for fname in fileNames:
        with open(fname) as infile:
            for line in infile:
                outfile.write(line)

http://docs.python.org/2/library/codecs.html#codecs.open http://docs.python.org/2/library/codecs.html#codecs.open

How about: 怎么样:

line.encode('utf-8')

in case it's not already encoded with utf-8. 如果尚未使用utf-8进行编码。 It should be though, when both files are initially utf-8. 应该是,当两个文件最初都是utf-8时。 You can also open the file in python with a given encoding: 您还可以使用给定的编码在python中打开文件:

file = open("C:\test.txt","r", encoding="utf-8")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM