#!/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数据集的更多相关文章

  1. Python读取MNIST数据集

    MNIST数据集获取 MNIST数据集是入门机器学习/模式识别的最经典数据集之一.最早于1998年Yan Lecun在论文: Gradient-based learning applied to do ...

  2. 使用shell/python获取hostname/fqdn释疑

    一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...

  3. python 获取日期

    转载   原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...

  4. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...

  5. python获取文件大小

    python获取文件大小 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import os # 字节bytes转化kb\m\g def formatSiz ...

  6. python 获取一个列表有多少连续列表

    python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:

  7. [python实用代码片段]python获取当前时间的前一天,前一周,前一个月

    python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...

  8. Python获取目录、文件的注意事项

    Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...

  9. Python 获取 网卡 MAC 地址

    /*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: ...

随机推荐

  1. ASP.NET 下拉列表绑定枚举类型值,不用再新建一个枚举表

    public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArg ...

  2. Func<T>与Action<T>委托泛型介绍

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

  3. [Spring] - 动态设置properties

    Spring的jar包用来做动态properties的getter/setter赋值方法: 1:需要的jar包: spring-beans-3.2.0.RC2.jar commons-logging- ...

  4. hadoop实战之分布式模式

    环境 192.168.1.101 host101 192.168.1.102 host102 1.安装配置host101 [root@host101 ~]# cat /etc/hosts |grep ...

  5. 解读(function($){...})(jQuery)

    function(arg){...}这就定义了一个匿名函数,参数为arg 而调用函数 时,是在函数后面写上括号和实参的,由于操作符的优先级,函数本身也需要用括号,即:(function(arg){.. ...

  6. [MySQL] SQL_ERROR 1032解决办法

    一.缘由: 在主主同步的测试环境,由于业务侧没有遵循同一时间只写一个点的原则,造成A库上删除了一条数据,B库上在同时更新这条数据. 由于异步和网络延时,B的更新event先到达A端执行,造成A端找不到 ...

  7. python(27)requests 爬取网页乱码,解决方法

    最近遇到爬取网页乱码的情况,找了好久找到了种解决的办法: html = requests.get(url,headers = head) html.apparent_encoding html.enc ...

  8. express - ejs使用介绍

    http://blog.sina.com.cn/s/blog_ad0672d60101l2ml.html 1.express中使用ejs var express = require('express' ...

  9. bootstrap-图文混排 media

    <!-- media 图文混排 media-left(right) 图片的区域 在左边显示(右边) media-body 内容区域 media-heading 内容区域里的标题 media-mi ...

  10. 配置SQL Server 2005 远程连接(转)

    方法如下:  一.为 SQL Server 2005 启用远程连接1. 单击"开始",依次选择"程序"."Microsoft SQL Server 2 ...