networkx使用笔记(四)之实战篇--数据的获取
2012-10-05 18:16
821 查看
公开的数据集
网络上针对网络的公开数据集较多,可以google斯坦福的SNAP,里面有分类的数据集这里我用了张华平老师公布的微博数据中的关注语料库
其为xml格式,记录方式如下:
<RECORDS> <RECORD> <person_id>10145</person_id> <guanzhu_id>10029</guanzhu_id> </RECORD> <RECORD> <person_id>10145</person_id> <guanzhu_id>10318</guanzhu_id> </RECORD> ... </RECORDS>可以利用python十分便利的解析xml,将里面的信息解析出来。为了简单,这里解析的信息,存储在数据库中。
数据的抽取
这里介绍包括对上述xml文件的信息抽取,以及对其他文件格式,例如txt、gml等文件信息的提取。1.xml
直接上代码from xml.etree import ElementTree import MySQLdb #从xml文件中读取person_id和guanzhu_id,首先进行id的映射(因为里面的编号不是从1开始的,为了方便,这里重新编号),存入id_map表中;然后id之间的关系,存入到id_info中 def read_xml(text=r''): root=ElementTree.fromstring(text) eitor=root.getiterator("RECORD") conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='python') cursor=conn.cursor() cursor.execute("""create table id_map(person_id BIGINT,id int )""") cursor.execute("""create table id_info(person_id int, guanzhu_id int)""") i=1 for e in eitor: person_id=int(e[0].text) guanzhu_id=int(e[1].text) if(cursor.execute("select * from id_map where person_id=%s",person_id)==0): cursor.execute("insert into id_map values(%s,%s)",[person_id,i]) person_id=i i=i+1 else: cursor.execute("select * from id_map where person_id=%s",person_id) person_id=cursor.fetchone()[1] if(cursor.execute("select * from id_map where person_id=%s",guanzhu_id)==0): cursor.execute("insert into id_map values(%s,%s)",[guanzhu_id,i]) guanzhu_id=i i=i+1 else: cursor.execute("select * from id_map where person_id=%s",guanzhu_id) guanzhu_id=cursor.fetchone()[1] cursor.execute("insert into id_info values(%s,%s)",[person_id,guanzhu_id]) #print person_id, guanzhu_id conn.commit() conn.close() return '''阅读xml之前,记得删除第一行''' read_xml(open("D:/data/guanzhu_relation.xml").read())
2. txt
这里针对如下格式:(两个数据之间用空格或tab隔开)0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10代码如下:
def read_text(file_name=r''): file_read=open(file_name) conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='python') cursor=conn.cursor() for line in file_read: ''' line=line.split(' ')如果是空格隔开 ''' line=line.split('\t') line=[line[0],line[1].replace('\n','')] cursor.execute('insert into ndw values (%s,%s)',[line[0],line[1]]) conn.commit() conn.close() file_read.close() return
3. gml文件
networkx可以直接通过函数从gml文件中读出数据。def read_gml(file_name=r''): file_read=open(file_name,'r') conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='python') cursor=conn.cursor() H=nx.read_gml(file_read) print H.number_of_nodes(), H.number_of_edges() for edge in H.edges():
'''这里检测是否有重复边存在''' if cursor.execute('select * from network where node1=%s and node2=%s',[edge[0],edge[1]])==0: cursor.execute('insert into network values (%s,%s)',[edge[0],edge[1]]) conn.commit() file_read.close() conn.close()
相关文章推荐
- 【JAVA笔记】使用ganymed-ssh2-build210.jar访问Linux虚拟主机获取数据
- SMP3.0学习笔记之四 使用OData Native SDK获取数据
- OpenLayers学习笔记8——使用servlet从mysql获取数据并标注
- Nutz使用笔记----获取插入数据id、自定义SQL
- 【学习笔记】day2数据存储和界面展现第一天 08_使用api获取内部存储空间路径09_files文件夹与cache文件夹
- networkx使用笔记(五)之实战篇--参数测量
- 《Flex 4实战》学习笔记:CH15 使用数据服务
- 学习笔记 Tianmao 篇 使用简单封装后的自定义OkHttp 获取json被GSON解析后的数据
- 从php获取json数据使用js读取显示到网页笔记
- JPA学习笔记---JPA数据的操作:增加,删除,修改,获取,使用JPQL进行查询
- android 开发笔记 使用Volley框架获取json数据出现中文乱码解决方案
- mxnet实战笔记(1) - 使用自己的图片数据训练CNN模型
- 通过wireshark获取应用接口并使用爬虫爬取网站数据(三)
- CUBRID学习笔记 39 net使用dataset 返回查询的数据
- SQL SERVER使用WITH TIES获取前几行数据
- 使用参数拦截器通过注解直接从JSON对象中获取数据
- C# 使用DataTime类获取时间数据
- 关于使用HandlerThread获取数据,并实现sqlite分页。
- EasyUI DataGrid表格双击获取该行数据—笔记
- JSON实战案例--使用JSON进行数据交换实例