37,737
社区成员
发帖
与我相关
我的任务
分享#!/usr/bin/python
# encoding: utf-8
import MySQLdb,MySQLdb.cursors
import string
import os
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
##
# 打开数据库连接
##conn = MySQLdb.connect("localhost","root","lab121","weibodata")
conn = MySQLdb.connect(host='localhost', user='root',\
passwd='lab121',db="weibodata",\
use_unicode=0, charset='gb2312',\
cursorclass=MySQLdb.cursors.SSCursor)
# 使用cursor()方法获取操作游标
cursor = conn.cursor()
cursor.execute('select index_id from userswith2000deg')
data = cursor.fetchall()
users = []
for row in data:
users.append(row[0])
print "get %s users" % len(users)
##conn.close()
folder = 'tests'
results = []
def get_paths(folder):
return (os.path.join(folder, f)
for f in os.listdir(folder)
if 'txt' in f)
def judgement(filename):
print filename
f = open(filename,'r')
for line in f:
line = line.split()
a = string.atoi(line[0])
b = string.atoi(line[1])
c = string.atoi(line[2])
if a in users:
if b in users:
results.append((a,b,c))
f.close()
if __name__ == '__main__':
folder = os.path.abspath(
'tests')
files = get_paths(folder)
pool = ThreadPool(3)
pool.map(judgement, files)
pool.close()
pool.join()
print results