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')

 

相關文章
相關標籤/搜索