#coding:utf8 import psycopg2 import codecs conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101") cursor = conn.cursor() exporttable = " is_firsttenderer " cursor.execute(" select A1.id,A1.content,B1.content from articles A1,articles_processed B1 where A1.id=B1.id and A1.id in(select distinct B.doc_id from "+exporttable+" A,entity_mention B,dd_graph_variables_holdout D where A.id=D.variable_id and A.entity_id=B.entity_id) ") all_doc_id = cursor.fetchall() for doc_id in all_doc_id: with codecs.open("export_article/"+str(doc_id[0])+"_a.html","w",encoding="utf8") as f: f.write(doc_id[1]) f.flush() f.close() with codecs.open("export_article/"+str(doc_id[0])+"_b.html","w",encoding="utf8") as f: f.write(doc_id[2]) f.flush() f.close()