python如何提取文章中的詞句 python提取有關鍵詞的句子怎么做
【第1句】:python 提取有關鍵詞的句子怎么做
高頻詞提?。?/p>
# !/usr/bin/python3
# coding:utf-8
import jieba.analyse
jieba.load_userdict('dict.txt') # dict.txt自定義詞典
content = open('kw.txt', 'rb').read()
tags = jieba.analyse.extract_tags(content, topK=10) # topK 為高頻詞數量
print("".join(tags))
【第2句】:用“python”怎么提取文件里的指定內容
python讀取文件內容的方法:
一.最方便的方法是一次性讀取文件中的所有內容并放置到一個大字符串中:
all_the_text = open('thefile.txt').read( )
# 文本文件中的所有文本
all_the_data = open('abinfile','rb').read( )
# 二進制文件中的所有數據
為了安全起見,最好還是給打開的文件對象指定一個名字,這樣在完成操作之后可以迅速關閉文件,防止一些無用的文件對象占用內存。舉個例子,對文本文件讀取:
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
不一定要在這里用Try/finally語句,但是用了效果更好,因為它可以保證文件對象被關閉,即使在讀取中發生了嚴重錯誤。
二.最簡單、最快,也最具Python風格的方法是逐行讀取文本文件內容,并將讀取的數據放置到一個字符串列表中:list_of_all_the_lines = file_object.readlines( )
這樣讀出的每行文本末尾都帶有""符號;如果你不想這樣,還有另一個替代的辦法,比如:
list_of_all_the_lines = file_object.read( ).splitlines( )
list_of_all_the_lines = file_object.read( ).split('')
list_of_all_the_lines = [L.rstrip('') for L in file_object]
【第1句】:Python
Python(英語發音:/?pa?θ?n/), 是一種面向對象、解釋型計算機程序設計語言,由Guido van Rossum于1989年底發明,第一個公開發行版發行于1991年,Python 源代碼同樣遵循 GPL(GNU General Public License)協議。
【第2句】:基本概念
Python(KK 英語發音:/'pa?θɑn/, DJ 英語發音:/?paiθ?n/)是一種面向對象、直譯式計算機程序設計語言,由Guido van Rossum于1989年底發明。
【第3句】:python如何提取字符串中的指定的內容
>> s = 'text=cssPath:"/ptlogin/v4/style/32",sig:"*uYPm*H3mpaOf3rs2M",clientip:"82ee3af631dd6ffe",serverip:"",version:"202404010930"'
>>> import re
>>> res = re.findall(r'sig:"([^"]+)"',s)
>>> res
['*uYPm*H3mpaOf3rs2M']
>>> res[0]
'*uYPm*H3mpaOf3rs2M'
【第4句】:python有哪些提取文本摘要的庫
一篇文章的內容可以是純文本格式的,但在網絡盛行的當今,更多是HTML格式的。
無論是哪種格式,摘要 一般都是文章 開頭部分 的內容,可以按照指定的 字數 來提取。【第2句】:純文本摘要 純文本文檔 就是一個長字符串,很容易實現對它的摘要提?。?!/usr/bin/env python# -*- coding: utf-8 -*-"""Get a summary of the TEXT-format document""" def get_summary(text, count): u"""Get the first `count` characters from `text` >>> text = u'Welcome 這是一篇關于Python的文章' >>> get_summary(text, 12) == u'Welcome 這是一篇' True """ assert(isinstance(text, unicode)) return text[0:count] if __name__ == '__main__': import doctest doctest.testmod() 【第3句】:HTML摘要 HTML文檔 中包含大量標記符(如
、、等等),這些字符都是標記指令,并且通常是成對出現的,簡單的文本截取會破壞HTML的文檔結構,進而導致摘要在瀏覽器中顯示不當。
在遵循HTML文檔結構的同時,又要對內容進行截取,就需要解析HTML文檔。在Python中,可以借助標準庫 HTMLParser 來完成。
一個最簡單的摘要提取功能,是忽略HTML標記符而只提取標記內部的原生文本。以下就是類似該功能的Python實現:#!/usr/bin/env python# -*- coding: utf-8 -*-"""Get a raw summary of the HTML-format document""" from HTMLParser import HTMLParser class SummaryHTMLParser(HTMLParser): """Parse HTML text to get a summary >>> text = u'Hi guys:This is a example using SummaryHTMLParser.' >>> parser = SummaryHTMLParser(10) >>> parser.feed(text) >>> parser.get_summary(u'。
') u'Higuys:Thi。' """ def __init__(self, count): HTMLParser.__init__(self) self.count = count self.summary = u'' def feed(self, data): """Only accept unicode `data`""" assert(isinstance(data, unicode)) HTMLParser.feed(self, data) def handle_data(self, data): more = self.count - len(self.summary) if more > 0: # Remove possible whitespaces in `data` data_without_whitespace = u''.join(data.split()) self.summary += data_without_whitespace[0:more] def get_summary(self, suffix=u'', wrapper=u'p'): return u'<{0}>{1}{2}{0}>'.format(wrapper, self.summary, suffix) if __name__ == '__main__': import doctest doctest.testmod() HTMLParser(或者 BeautifulSoup 等等)更適合完成復雜的HTML摘要提取功能,對于上述簡單的HTML摘要提取功能,其實有更簡潔的實現方案(相比 SummaryHTMLParser 而言):#!/usr/bin/env python# -*- coding: utf-8 -*-"""Get a raw summary of the HTML-format document""" import re def get_summary(text, count, suffix=u'', wrapper=u'p'): """A simpler implementation (vs `SummaryHTMLParser`). >>> text = u'Hi guys:This is a example using SummaryHTMLParser.' >>> get_summary(text, 10, u'。
') u'Higuys:Thi。' """ assert(isinstance(text, unicode)) summary = re.sub(r'<.*?>', u'', text) # key difference: use regex summary = u''.join(summary.split())[0:count] return u'<{0}>{1}{2}{0}>'.format(wrapper, summary, suffix) if __name__ == '__main__': import doctest doctest.testmod()。
【第5句】:python怎么獲取公文里的內容
最方便的方法是一次性讀取文件中的所有內容并放置到一個大字符串中:
all_the_text = open('thefile.txt').read( )
# 文本文件中的所有文本
all_the_data = open('abinfile','rb').read( )
# 二進制文件中的所有數據
為了安全起見,最好還是給打開的文件對象指定一個名字,這樣在完成操作之后可以迅速關閉文件,防止一些無用的文件對象占用內存。
【第6句】:如何用Python分析一篇文章的關鍵詞
應該用Python的正則表達式模塊re
示例:
import re
with open('test.txt','r') as txt:
f = txt.read()
print re.match('正則表達式/關鍵詞',f)
具體可以多了解一下這個模塊,查詢有三種方法,一個是match匹配,也是比較常用的
然后還有search和findall
個人覺得這個人的正則表達式介紹文章還不錯,推薦你參考:
/theminority/article/details/7629227