Advertisement
Typhoon

Extract data from PostgreSQL

Jan 29th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import psycopg2
  5. import sys
  6.  
  7. try:
  8.     offset = 0
  9.     while offset < 9000000:
  10.         #print "Current OFFSET : " + str(offset)
  11.         con = psycopg2.connect(database='postgres', user='postgres', password='postgres')
  12.         cur = con.cursor()
  13.         sql='SELECT name,content FROM my_db LIMIT 200 OFFSET {0}'.format(offset)
  14.         cur.execute(sql)
  15.         dataset = cur.fetchall()
  16.  
  17.         for row in dataset:
  18.             nazov=row[0].upper().decode('utf8')
  19.             obsah=row[1].lower().decode('utf8')
  20.             nazov=nazov.encode('utf8').replace("í","Í").replace("š","Š").replace("č","Č").replace("ľ","Ľ") \
  21.                                       .replace("ž","Ž").replace("ý","Ý").replace("é","É").replace("á","Á") \
  22.                                       .replace("ť","Ť").replace("ú","Ú").replace("ň","Ň").replace("ó","Ó") \
  23.                                       .replace("ô","Ô").replace("ä","Ä").replace("ě","Ě").replace("ü","Ü") \
  24.                                       .replace("ö","Ö").replace("ů","Ů").replace("ř","Ř") # Quick&Dirty :) #
  25.  
  26.             print "\n" + "-----" + "\n" + nazov + "\n"
  27.             print obsah
  28.         offset = offset +200
  29.  
  30. except psycopg2.DatabaseError, e:
  31.     print 'Error %s' % e
  32.     sys.exit(1)
  33.  
  34. finally:
  35.     if con:
  36.         con.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement