正在python unicode中修改的unicode文本

正在python unicode中修改的unicode文本,python,unicode,Python,Unicode,我想读取包含unicode数据的文件,进行一些处理并写入另一个文件 # -*- coding: utf-8 -*- import sys import codecs def main(big_filename, small_filename): print "Big file ", big_filename print "Small file ", small_filename pattern1 = u'CreationDate="2008' pattern

我想读取包含unicode数据的文件,进行一些处理并写入另一个文件

# -*- coding: utf-8 -*-
import sys
import codecs


def main(big_filename, small_filename):

    print "Big file ", big_filename
    print "Small file ", small_filename
    pattern1 = u'CreationDate="2008'
    pattern2 = u'CreationDate="2014'

    small_f = codecs.open(small_filename, 'w', encoding='utf-8')
    small_f.write('<?xml version="1.0" encoding="utf-8"?>\n')
    small_f.write("<posts>\n")

    cnt = 0
    big_f = codecs.open(big_filename, 'r', encoding='utf-8')
    for line in big_f:
        #line = line.decode('utf-8')
        if line.find(pattern1) != -1 or line.find(pattern2) != -1:
            small_f.write(line)
            cnt = cnt + 1
            if cnt%1000:
                print cnt, " records written"

    small_f.write("<\posts>\n")        
    small_f.close()

if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2])
和小文件名中的文本

Other hardware architectures fall back to the system’s timer, which is typically set to 100 Hz.

但是我明白了,一些unicode ext在小文件名中被修改了。有人能告诉我如何解决这个问题吗?

字符串本身就包含encode方法。试试这个:

读取小文件并作为变量转储,如下所示:small\u Read=opensmall\u filename.Read 用新的空白文件覆盖该文件:small\u file=opensmall\u文件名“w” 使用编解码器完全按照您所做的方式编写:

small\u f=codecs.opensmall\u文件名'w',encoding='utf-8' 小字体。写“\n” 小型\u f.写入\n

现在使用utf-8编码编写文件内容,如下所示:

小文件。写入全部读取。编码“utf-8” 小文件。关闭


UH哪一个文件是第一个,哪一个是第二个?你能提供这两个文件在反勾符号位置周围的十六进制转储吗?这个问题被修改为正确的文件名,我已经复制了修改数据的完整句子。该文件是stackoverflow问题的xml转储。标题看起来像这样,我无法读取整个文件并将其作为变量转储。它是30GB。试着用zlib压缩它或实现哈夫曼加密。
Other hardware architectures fall back to the system’s timer, which is typically set to 100 Hz.