python实现获取文件列表中每一个文件keyword
功能描写叙述:
获取某个路径下的全部文件,提取出每一个文件里出现频率最高的前300个字。保存在数据库其中。
前提。你须要配置好nltk
#!/usr/bin/python
#coding=utf-8
'''
function : This script will create a database named mydb then abstract keywords of files of privacy police. author : Chicho date : 2014/7/28 running : python key_extract.py -d path_of_file
''' import sys,getopt
import nltk
import MySQLdb
from nltk.corpus import PlaintextCorpusReader corpus_root = "" if __name__ == '__main__': opts,args = getopt.getopt(sys.argv[1:], "d:h","directory=help") #get the directory
for 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_file
corpus_root = sys.argv[1]
''' # corpus_root is the directory of files of privacy policy, all of the are html files
filelists = PlaintextCorpusReader(corpus_root, '.*') #get the files' list
files = filelists.fileids() #connect the database
conn = MySQLdb.connect(host = 'your_personal_host_ip_address', user = 'rusername', port =your_port, passwd = 'U_password')
#get the cursor
curs = 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 e i = 0
for 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 policy
wordlist = [w for w in filelists.words(privacyfile) if w.isalpha() and len(w)>2] # get the keywords
fdist = nltk.FreqDist(wordlist)
vol = fdist.keys()
key_num = len(vol)
if key_num > 300:
key_num = 300
for j in range(key_num):
sql = "update filekeywords set " + "key" + str(j) + "=" + "'" + vol[j] + "' where id=" + str(i)
curs.execute(sql)
i = i + 1 conn.commit()
curs.close()
conn.close()
转载注明出处:http://blog.csdn.net/chichoxian/article/details/42003603
python实现获取文件列表中每一个文件keyword的更多相关文章
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- python实现获取文件夹中的最新文件
实现代码如下: #查找某目录中的最新文件import osclass FindNewFile: def find_NewFile(self,path): #获取文件夹中的所有文件 lists = os ...
- 基于Python——实现解压文件夹中的.zip文件
[背景]当一个文件夹里存好好多.zip文件需要解压时,手动一个个解压再给文件重命名是一件很麻烦的事情,基于此,今天介绍一种使用python实现批量解压文件夹中的压缩文件并给文件重命名的方法—— [代码 ...
- 每日学习心得:SharePoint 为列表中的文件夹添加子项(文件夹)、新增指定内容类型的子项、查询列表中指定的文件夹下的内容
前言: 这里主要是针对列表中的文件下新增子项的操作,同时在新建子项时,可以为子项指定特定的内容类型,在某些时候需要查询指定的文件夹下的内容,针对这些场景都一一给力示例和说明,都是一些很小的知识点,希望 ...
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- python 将指定文件夹中的指定文件放入指定文件夹中
import os import shutil import re #获取指定文件中文件名 def get_filename(filetype): name =[] final_name_list = ...
- 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world
一.题目 编写一个内核模块,在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world.内核版本要求2.6.18 二.实验环境 物理主机:w ...
- 获取SD卡中的音乐文件
小编近期在搞一个音乐播放器App.练练手: 首先遇到一个问题.怎么获取本地的音乐文件? /** * 获取SD卡中的音乐文件 * * @param context * @return */ public ...
- 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来
/*4.创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来*/ #import <Foundation/Foundation.h>#defin ...
随机推荐
- ECNUOJ 2574 Principles of Compiler
Principles of Compiler Time Limit:1000MS Memory Limit:65536KBTotal Submit:473 Accepted:106 Descripti ...
- 洛谷 P3467 [POI2008]PLA-Postering
P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...
- [DLX精确覆盖+打表] hdu 2518 Dominoes
题意: 就是给12种图形,旋转,翻折.有多少种方法构成n*m=60的矩形 思路: 裸的精确覆盖.就是建图麻烦 个人太挫,直接手写每一个图形的各种形态 须要注意的是最后的答案须要除以4 代码: #inc ...
- WireGuard 隧道的安装和使用,测试地址 ca.6tu.me
WireGuard 端到端平等的网络隧道,测试地址 ca.6tu.me --------------------------------------------- 服务器:远程 Ubuntu18 , ...
- linux log日志解析
linux log日志解析 其实,可以说成是监控系统的记录,系统一举一动基本会记录下来.这样由于信息非常全面很重要,通常只有 root 可以进行视察!通过登录文件(日志文件)可以根据屏幕上面的错误 ...
- 81.node.js前端html时页面格式错乱解决办法
var http = require("http"); var url = require("url"); var fs = require("fs& ...
- Redis封装之Set
RedisSetService: /// <summary> /// Set:用哈希表来保持字符串的唯一性,没有先后顺序,存储一些集合性的数据 /// 1.共同好友.二度好友 /// 2. ...
- PHP写文件到指定位置
<?php $fp = fopen("output.json", "r+"); $flag = fseek($fp, -3, SEEK_END); if( ...
- uni-app 地图初用 map
一.uni-app 地图初用 map 代码如下: <template> <view> <!-- <page-head :title="title" ...
- C# ArcGIS Engine 使当前选中的操作失效(清除当前鼠标事件)
问题描述: 前提:我用的ENGINE9.3开发,拖了一个ToolbarControl控件,加了一些常用操作的工具(平移,放大,缩小)在上边. 问题:我做了一个增加点的按钮.当我平移完地图之后,点击增加 ...