python把myinput.txt中的文字 分词,保存在myoutput.txt中,并制成图片


#-*- coding: utf-8 -*-
#code:myhaspl@qq.com
#12-1.py
import sys
reload(sys)
sys.setdefaultencoding("gbk")
sys.path.append("../")
import jieba

def splitSentence(inputFile, outputFile):
    fin = open(inputFile, 'r')                                  #以读的方式打开文件
    fout = open(outputFile, 'w')                                #以写得方式打开文件

    for eachLine in fin:
        line = eachLine.strip().decode('gbk', 'ignore')       #去除每行首尾可能出现的空格,并转为Unicode进行处理
        wordList = list(jieba.cut(line,cut_all=False))                        #用结巴分词,对每行内容进行分词
        outStr = ''
        for word in wordList:
            outStr += word
            outStr +=chr(10)+chr(10)+ ' '
        fout.write(outStr.strip().encode('gbk') + '\n')       #将分词好的结果写入到输出文件


    fin.close()
    fout.close()

splitSentence('myInput.txt', 'myOutput.txt')

import os  
import pygame  
from pygame.locals import *  
 
pygame.init()
#font = pygame.font.SysFont('SimHei', 14)

fz=open('myOutput.txt','r')

for i,eachLine in enumerate(fz):
       if len(eachLine) 1 :
        text = eachLine.strip().decode('gbk', 'ignore')       #去除每行首尾可能出现的空格,并转为Unicode进行处理
        font = pygame.font.SysFont('SimHei', 14)
        #print text
        ftext = font.render(text.decode('gbk'), True, (0, 0, 0), (255, 255, 255))  
        #print i
        
        pygame.image.save(ftext,'e:\\picz\\pic\\'+str(i)+' '+eachLine.strip()+'.jpg')  



fz.close()





 
   

最新回复(0)
/jishucpWcR_2FsgBuWpEsQxH1IHh643KPdODhrz9Gv78w_3D_3D4858625
8 简首页