#coding:utf8 import psycopg2 import codecs import re def havelook(): conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101") cursor = conn.cursor() sql = " select A.entity_text,A.begin_index,A.end_index,B.tokens from entity_mention A,sentences B where A.entity_type='money' and A.doc_id=B.doc_id and A.sentence_index=B.sentence_index " cursor.execute(sql) rows = cursor.fetchmany(1000) MAX_NUM = 7 with codecs.open("look.txt", "w", encoding="utf8") as f: while(rows): for row in rows: entity_text = row[0] begin_index = row[1] end_index = row[2] tokens = row[3] if begin_index>MAX_NUM: begin = begin_index-MAX_NUM else: begin = 0 if end_index+MAX_NUM