为什么不写我的文件python

时间:2018-03-29 23:33:45

标签: python file csv

尝试将多个文件中的信息格式化为单个csv文件,任何“垃圾邮件”都写得很好,但文件中没有“ham”,但是“print('ham ....”正在调用并输出到控制台。

正在调用我的写入火腿行(在else之后),但它没有被写入文件

import os

root_dir = '../res'
output_file = '../res/mass.csv'

for folder, subs, files in os.walk(root_dir):
     with open(output_file, 'w') as dest:
         for filename in files:
            if filename != 'Summary.txt':
                with open(os.path.join(folder, filename), 'r') as src:
                    if 'spam' in filename:
                        dest.write(('spam\t' + src.readline())
                                    .decode('utf-8', 'ignore').encode('utf-8'))
                    else:
                        print ('ham\t' + src.readline()).decode('utf-8', 'ignore').encode('utf-8')
                        dest.write(('ham\t' + src.readline())
                                    .decode('utf-8', 'ignore').encode('utf-8'))

文件输出是一堆没有火腿的垃圾邮件

垃圾邮件主题:立即声明您的获奖优惠!

垃圾邮件主题:获胜通知

0 个答案:

没有答案