python中使用文件的读取编码问题和简单正则使用(二) python中使用文件的读取和简单正则使用(一)

写这个简单的正则匹配呢,主要是让你们能够参考取本身文本中想要的东西,好比我如今的文本内容是这样的html

我想要把<Sentiment>和</Sentiment>之间的neg和pos提取出来,文件基本操做参考上一篇python中使用文件的读取和简单正则使用(一)python

下面上代码了post

# coding=utf-8
import re 
fopen=open('monitor.txt','r')
lines=[]
lines=fopen.readlines()
fopen.close()
fout=open('1.txt','w')
for line in lines:
    line.decode('gbk','ignore').encode('utf-8') 
    match=re.findall(".*<Sentiment>(.*)</Sentiment>.*",line)
    for x in match:
        fout.write(x)
        fout.write('\n')

 

相关文章
相关标签/搜索