python编码问题

#coding:utf-8

import os

 

'''

将案号与数字对应上

'''

dirname = u'1_xunwenbilu/'  #############获得指定文件夹下面的所有文件的名字

list1 = os.listdir(dirname)

print(list1)

print 222

with open('1_xunwenbilu/1.txt','w') as fp:

    tag = 10000000

    for e1 in list1:

        print(e1, type(e1))

        # fp.write('{}\n'.format(e1))

        if e1.find('.txt') == -1 or len(e1) < 15:

            continue

        fp.write(u'{}\t{}.txt\n'.format(e1.split('_')[0], tag).encode('utf8'))

        #### 这样可以使unicode变成utf8,写入文件后的文件的编码格式也为utf8

####若改成fp.write('{}\t{}.txt\n'.format(e1.split('_')[0], tag)) 则写入文件后的文件的格式为utf8

 

        str1 = ''

        with open('1_xunwenbilu/'+e1) as f1:

            for line in f1.readlines():

                line = line.strip()

                if line == '':

                    continue

                if str1 == '':

                    str1 = line

                else:

                    str1 = str1 + '\n' + line

            with open('1_xunwenbilu/xunwenbilu_result/'+str(tag) + '.txt', 'w') as f2:

                f2.write(str1)

 

        tag += 1

你可能感兴趣的:(python,编码,python,编码)