從文字中提取特定資訊

2021-09-19 16:52:41 字數 2431 閱讀 5576

嘗試了兩種方法,正規表示式提取效果更佳

#! /usr/bin/env python

# -*- coding: utf-8 -*-

'''方法1:採用位置引數來提取,效果不佳'''

##file_data=

##with open('待處理文字.txt',encoding='utf-8') as f:

##    data=f.read().split('  ')

##    file={}

##    while(len(data)):

##        file['name']=data.pop(0)

##        file['director']=data.pop(0)

##        file['actor']=data.pop(0)

##with open('電影列表.txt','w') as f:

##    for file in file_data:

##        f.write(file['name']+'\n')

##        f.write(file['director']+'\n')

##        f.write(file['actor']+'\n')

'''方法2:採用正規表示式,提取很好。'''

import repattern=re.compile(r'\d(?p\s+)  導演:(?p[\s ]+)  主演:(?p[\s ]+)')with open('待處理文字.txt',encoding='utf-8') as f:    data=f.read()with open('電影列表.txt','w') as f:    for index,(name,director,actor) in enumerate(pattern.findall(data)):        f.write('no:'+str(index+1)+'\n')        f.write('片名:'+name+'\n')        f.write('導演:'+director+'\n')        f.write('演員:'+actor+'\n')##        print('no:'+str(index+1))

##        print('片名:'+name)

##        print('導演:'+director)

##        print('演員:'+actor)

嘗試了兩種方法,正規表示式提取效果更佳

#! /usr/bin/env python

# -*- coding: utf-8 -*-

'''方法1:採用位置引數來提取,效果不佳'''

##file_data=

##with open('待處理文字.txt',encoding='utf-8') as f:

##    data=f.read().split('  ')

##    file={}

##    while(len(data)):

##        file['name']=data.pop(0)

##        file['director']=data.pop(0)

##        file['actor']=data.pop(0)

##with open('電影列表.txt','w') as f:

##    for file in file_data:

##        f.write(file['name']+'\n')

##        f.write(file['director']+'\n')

##        f.write(file['actor']+'\n')

'''方法2:採用正規表示式,提取很好。'''

import repattern=re.compile(r'\d(?p\s+)  導演:(?p[\s ]+)  主演:(?p[\s ]+)')with open('待處理文字.txt',encoding='utf-8') as f:    data=f.read()with open('電影列表.txt','w') as f:    for index,(name,director,actor) in enumerate(pattern.findall(data)):        f.write('no:'+str(index+1)+'\n')        f.write('片名:'+name+'\n')        f.write('導演:'+director+'\n')        f.write('演員:'+actor+'\n')##        print('no:'+str(index+1))

##        print('片名:'+name)

##        print('導演:'+director)

##        print('演員:'+actor)

從html富文字中提取純文字

其實從html富文字中提取純文字很簡單,富文字基本上是使用html標籤給文字加上豐富多彩的樣式。所以只需要將富文字字串中的 標籤剔除,即可得到純文字。我們可以使用正規表示式,來匹配所有的html標籤,並替換成空字元,如下 html剔除富文字標籤,留下純文字 function get text htm...

從文字中提取所有的單詞

比如 hello world hello everyone,my name is caozhy 輸出 caozhy everyone hello ismy name world class dictgen public node public ienumerable string get publi...

搜尋之路 c 從html中提取文字

直接封裝成乙個類的,用起來還挺方便的 using system using system.data using system.configuration using system.web using system.web.security using system.web.ui using syst...