python获取DBLP数据集
#!/usr/bin/python
# -*- coding: UTF-8 -*- import xml.sax
import io, sys paper_tags = ('article', 'inproceedings', 'proceedings', 'book', 'incollection', 'phdthesis', 'mastersthesis', 'www') sub_tags = ('publisher', 'journal', 'booktitle') ret = [] class DBLPHandler(xml.sax.ContentHandler): def __init__(self):
self.id = 1
self.reset() def reset(self):
self.dup_article = 0
self.curtag = None
self.author = ''
self.title = ''
self.pages = ''
self.year = ''
self.volume = ''
self.journal = ''
self.number = ''
self.url = ''
self.ee = '' def write_to_file(self, filename):
file_object = file(filename, 'a+')
for line in ret:
file_object.write(line.encode('utf8'))
#file_object.write('\n')
file_object.close() def record_row(self):
ret.append(u''.join((self.author, self.title, self.year, self.pages, self.journal, self.ee, '\n')).replace(' ', ''))
#ret.append(self.author + self.title + self.year + self.pages+ self.journal + self.ee)
#ret.append((self.author, self.title, self.year, self.pages, self.journal, self.ee))
#print (self.author, self.title, self.year, self.pages) def startElement(self, tag, attributes):
if tag != None and len(tag.strip()) > 0:
if tag == 'article':
self.dup_article += 1
self.curtag = tag def endElement(self, tag):
if tag != None and len(tag.strip()) > 0:
if tag == 'article':
self.record_row()
self.reset() def characters(self, content):
if content != '\n':
if self.curtag == "title":
self.title = content.strip()
elif self.curtag == "author":
self.author = content.strip()
elif self.curtag == "year":
self.year = content.strip()
elif self.curtag == "ee":
self.ee = content.strip()
elif self.curtag == "journal":
self.journal = content.strip()
elif self.curtag == "pages":
self.pages = content.strip()
elif self.url == "url":
self.url = content.strip()
elif self.number == "number":
self.number = content.strip()
elif self.number == "volume":
self.volume = content.strip() if (__name__ == "__main__"):
filename = 'dblp.xml'
if len(sys.argv) == 2:
filename = sys.argv[1]
# 创建一个 XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0) # 重写 ContextHandler
Handler = DBLPHandler()
parser.setContentHandler(Handler) parser.parse(filename)
print 'Parser Complete!'
Handler.write_to_file('out')
另外附处理DNA数据的脚本程序:
lens_DNA = [0, 1000, 2000, 2500, 500, 1000, 1500, 2000, 2500]
lens_DBLP = [0, 40, 120, 200, 40, 80, 120, 160, 200] file_id = 1
LINE_MAX = 100 class DNA_Handler:
def __init__(self):
self.strn = '' def write_to_file(self, filename):
file_object = open(filename, 'a+')
file_object.write(self.strn)
file_object.close() def read_file(self, filename):
fo = open(filename, 'r')
line = fo.readline()
self.strn = ''
file_id = 1
cnt_lines = 0
while line and file_id < 9:
line = line.replace('\n', '')
self.strn += line
if len(self.strn) > lens_DNA[file_id]:
self.strn = self.strn[0: lens_DNA[file_id]] + '\n'
print self.strn
if file_id <= 3:
self.write_to_file('DNA_N' + str(file_id))
else:
self.write_to_file('DNA_M' + str(file_id - 3))
self.strn = ''
cnt_lines += 1
if cnt_lines >= LINE_MAX:
file_id += 1
cnt_lines = 0
line = fo.readline()
fo.close()
print 'read_finished!' class DBLP_Handler: def __init__(self):
self.strn = '' def write_to_file(self, filename):
file_object = open(filename, 'a+')
file_object.write(self.strn)
file_object.close() def read_file(self, filename):
fo = open(filename, 'r')
line = fo.readline()
self.strn = ''
file_id = 1
cnt_lines = 0
while line and file_id < 9:
line = line.replace('\n', '')
self.strn += line
if len(self.strn) > lens_DBLP[file_id]:
self.strn = self.strn[0: lens_DBLP[file_id]] + '\n'
print self.strn
self.write_to_file('DBLP_' + str(file_id))
self.strn = ''
cnt_lines += 1
if cnt_lines >= LINE_MAX:
file_id += 1
cnt_lines = 0
line = fo.readline()
fo.close()
print 'read_finished!' if (__name__ == '__main__'): dh = DNA_Handler()
dh.read_file('human_dna.fa')
'''
bblp_h = DBLP_Handler()
bblp_h.read_file('DBLP_data')
'''
python获取DBLP数据集的更多相关文章
- Python读取MNIST数据集
MNIST数据集获取 MNIST数据集是入门机器学习/模式识别的最经典数据集之一.最早于1998年Yan Lecun在论文: Gradient-based learning applied to do ...
- 使用shell/python获取hostname/fqdn释疑
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...
- python 获取日期
转载 原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...
- python获取文件大小
python获取文件大小 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import os # 字节bytes转化kb\m\g def formatSiz ...
- python 获取一个列表有多少连续列表
python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:
- [python实用代码片段]python获取当前时间的前一天,前一周,前一个月
python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...
- Python获取目录、文件的注意事项
Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...
- Python 获取 网卡 MAC 地址
/*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: ...
随机推荐
- 为 C# 代码生成 API 文档(译)
原文地址:http://broadcast.oreilly.com/2010/09/build-html-documentation-for-y.html#comments Sandcastle 功能 ...
- js 日期按年月日加减
<script> function isleapyear(year) { if(parseInt(year)%4==0 && parseInt(year)%100!=0)r ...
- mesos INSTALL
$ git clone https://github.com/mesosphere/playa-mesos $ cd playa-mesos $ vagrant up --provision
- HTTP Proxy Servlet 代理服务使用
java servlet 代理服务器 1. 使用 maven 依赖 <dependency> <groupId>org.mitre.dsmiley.httpproxy< ...
- Appium使用PageFactory初始化对象时报空指针错误
自己的测试框架里面,每个app页面都要初始化appium field,所以想到使用一个静态的变量,后来初始化一个页面对象时总是报空指针. 在网上找了好多材料,看着没有什么区别.后来在github上面看 ...
- 【C】 02 - 程序结构和预处理
在正式进入C的语法之前,有必要对其整体外观和组成元素作一个浏览.这部分内容对大多数人是比较陌生的,但它们却是C的起点和骨架.而这些内容涉及的背景或细节又可以展开为专门的课题,这里也只是浅尝则止,说明个 ...
- Deepin下phpunit安装,以及执行过程中所遇到的问题
Deepin下phpunit安装,以及执行过程中所遇到的问题 安装phpunit步骤 wget https://phar.phpunit.de/phpunit.phar chmod +x phpuni ...
- 电子面单纸打印时固定高度18cm,到底是多少px
点评:A4纸竖向打印,html网页页面的宽度设置成多少?这个问题是我们大家所疑惑的,于是网上搜集整理下,希望可以帮助你们 最近开发项目时遇到了网页打印的问题,这是问题之二,打印宽度设置 在公制长度单位 ...
- 墨菲定律-Murphy's Law (转载)
墨菲定律 “墨菲定律”(Murphy's Law)亦称莫非定律.莫非定理.或摩菲定理,是西方世界常用的俚语. “墨菲定律”:事情往往会向你所想到的不好的方向发展,只要有这个可能性.比如你衣袋里有两把钥 ...
- Sublime Text 3 插件安装及Vim 模式设置
1.安装Sublime Text 3 下载安装:http://www.sublimetext.com/3 Package Control安装:https://sublime.wbond.net/in ...