功能描述:

获取某个路径下的所有文件,提取出每个文件中出现频率最高的前300个字。保存在数据库当中。

前提,你需要配置好nltk

#!/usr/bin/python
#coding=utf-8
'''
function : This script will create a database named mydb thenabstract keywords of files of privacy police.author    : Chichodate      : 2014/7/28running   : python key_extract.py -d path_of_file
'''import sys,getopt
import nltk
import MySQLdb
from nltk.corpus import PlaintextCorpusReadercorpus_root = ""if __name__ == '__main__':opts,args = getopt.getopt(sys.argv[1:], "d:h","directory=help")#get the directoryfor op,value in opts:if op in ("-d", "--directory"):corpus_root = value#actually, the above method to get  a directory is a little complicated,you can#do like this'''the input include you path and use sys.argv to get the path ''''''running : python key_extract.py you path_of_filecorpus_root = sys.argv[1]'''# corpus_root is the directory of files of privacy policy, all of the are html filesfilelists = PlaintextCorpusReader(corpus_root, '.*')#get the files' listfiles = filelists.fileids()#connect the databaseconn = MySQLdb.connect(host = 'your_personal_host_ip_address', user = 'rusername', port =your_port, passwd = 'U_password')#get the cursorcurs = conn.cursor()conn.set_character_set('utf8')curs.execute('set names utf8')curs.execute('SET CHARACTER SET utf8;')curs.execute('SET character_set_connection=utf8;')'''conn.text_factory=lambda x: unicode(x, 'utf8', "ignore")#conn.text_factory=str    '''   # create a database named mydb'''try:curs.execute("create database mydb")except Exception,e:print e'''conn.select_db('mydb')try:for i in range(300):sql = "alter table filekeywords add " + "key" + str(i) + " varchar(45)"curs.execute(sql)except Exception,e:print ei = 0for privacyfile in files:#f = open(privacyfile,'r', encoding= 'utf-8')sql = "insert into filekeywords set id =" + str(i)curs.execute(sql)sql = "update filekeywords set name =" + "'" + privacyfile + "' where id= " + str(i)curs.execute(sql)# get the words in privacy policywordlist = [w for w in filelists.words(privacyfile) if w.isalpha() and len(w)>2]# get the keywordsfdist = nltk.FreqDist(wordlist)vol = fdist.keys()key_num = len(vol)if key_num > 300:key_num = 300for j in range(key_num):sql = "update filekeywords set " + "key" + str(j) + "=" + "'" + vol[j] + "' where id=" + str(i)curs.execute(sql)i = i + 1conn.commit()curs.close()conn.close()

转载注明出处:http://blog.csdn.net/chichoxian/article/details/42003603

python实现获取文件列表中每个文件关键字相关推荐

  1. python 获取文件列表_Python3 - 获取文件夹中的文件列表

    问题 获取文件系统中某个目录下的所有文件列表. 解决方案 使用 os.listdir() 函数来获取某个目录中的文件列表,比如: import os file_name = os.listdir('/ ...

  2. Python 获取文件夹中的文件列表

    获取文件夹中的文件列表 问题 你想获取文件系统中某个目录下的所有文件列表. 解决方案 使用 os.listdir() 函数来获取某个目录中的文件列表: import os names = os.lis ...

  3. python3 获取文件夹中的文件列表

    问题 获取文件系统中某个目录下的所有文件列表. 解决方案 使用 os.listdir() 函数来获取某个目录中的文件列表,比如: import osfile_name = os.listdir('/U ...

  4. python单稳态触发_使用 方法可以获取一个列表中最大元素的值。_学小易找答案...

    [填空题]若在列表的最后添加新元素,则可以直接使用列表的 方法. [填空题]将一个线程的 属性设置为True,则该线程为守护线程. [填空题]是在调用函数时函数名后面的一对小括号中给出的参数列表. [ ...

  5. 获取文件夹中所有文件的文件名[重复]

    本文翻译自:Getting the filenames of all files in a folder [duplicate] Possible Duplicate: 可能重复: Read all ...

  6. python 文件夹中的文件批量处理 高通道tif图片转换成jpg格式

    在数据集的制作中,往往涉及到 文件夹中文件的批量处理操作,而首要的任务便是将获得的数据进行重命名以方便批量读取,随后再进行操作. 一.文件批量重命名 文件夹的批量处理操作关键在于用os库的os.lis ...

  7. 微信小程序如何获取云存储中指定文件夹下所有图片

    微信小程序可以使用小程序云开发的云函数来获取云存储中指定文件夹下的所有图片. 首先,你需要在云开发控制台中创建一个云函数,然后在函数代码中调用云存储 API 获取指定文件夹下的所有文件. 具体来说,你 ...

  8. 在swt中获取jar包中的文件 uri is not hierarchical

    uri is not hierarchical 学习了:http://blog.csdn.net/zdsdiablo/article/details/1519719 在swt中获取jar包中的文件: ...

  9. 百度云android隐藏空间,一招教你使手机端百度网盘中的隐藏空间在文件列表中显示出来...

    一招教你使手机端百度网盘中的隐藏空间在文件列表中显示出来 相信很多小伙伴都有在使用手机百度网盘,其中的隐藏空间如何才能在文件列表中显示出来呢?方法很简单,下面小编就来为大家介绍. 具体如下: 1. 首 ...

最新文章

  1. android exchange服务,带有“ Service com.android.exchange.ExchangeService
  2. linux c 各头文件作用总结
  3. JAVA实现简单链表操作
  4. 全球及中国EDM放电加工机行业运营盈利前景与渠道分析报告2022版
  5. shiro缓存管理时报错java.lang.ClassCastException: com.xxx.User cannot be cast to com.xxx.User
  6. 枚举枚举和修改“最终静态”字段的方法
  7. 编程是一门实践性的科学
  8. 佳能g2810打印机扫描怎么用_办公用这款佳能彩色激光打印机无线MF643CDW就够了!...
  9. php mysql时间倒序,php mysql时间已过去计算
  10. centos7安装java6_CentOS7.6安装openjdk
  11. Linux下的高级文件权限ACL
  12. JAVA 列表输入学生的信息
  13. 广州市职称计算机应用考试,职称计算机考试
  14. 阿里云容器镜像加速器
  15. 到了2017还在苦等房价下跌的人,你们可以醒醒了!
  16. 学计算机打字一段话,如何快速学会电脑打字
  17. drools规则语言指南(五)规则条件和行为
  18. bzoj 4480: [Jsoi2013]快乐的jyy
  19. 【蓝桥杯】大臣的旅费
  20. 坚持玩游戏为什么会这么容易

热门文章

  1. 今年我读了四个开源项目的源码,来分享下心得
  2. 震精!Spring Boot内存泄露,排查竟这么难!
  3. 说时间不够用,CTO怒了!让我学习:GTD时间管理,番茄工作法,三八理论
  4. 原创 | CRUD更要知道的Spring事务传播机制
  5. 阿里资深Leader:组建技术团队的一些思考
  6. 盘点Java框架常用的3大底层技术!
  7. 靠纯技术是否能渡过中年危机
  8. OKR和KPI有什么区别?一篇文章搞懂
  9. ZED~Windows
  10. crt 运行时库dll跨模块传递crt对象,出现的崩溃问题