Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import psycopg2
  2. import json
  3. import html
  4.  
  5. #conn = psycopg2.connect('postgres://periphery_robertb4:Periphery@periphery.web.illinois.edu:5432/periphery_LittleFreeLibrary', sslmode='require')
  6. conn = psycopg2.connect(user="periphery_apande9",
  7.                         password="Periphery",
  8.                         host="periphery.web.illinois.edu",
  9.                         port="5432",
  10.                         database="peripery_LittleFreeLibrary",
  11.                         sslmode="require")
  12. conn.autocommit = True
  13. cur = conn.cursor()
  14.  
  15.  
  16. cur.execute('drop table if exists Charters;')
  17. cur.execute('create table Charters(libname text, image text, stewname text, email text, num text, story text, emailonmap bool, nameonmap bool, lat real, lon real, street text, city text, state text, country text, zipcode text, cid text);')
  18.  
  19. with open("369_miles.json", encoding="utf8") as read_file:
  20.     charters = json.load(read_file)
  21.     valid = 0
  22.     skipped = 0
  23.     for charter in charters:
  24.         try:
  25.             library = charter["library"]
  26.             libname = html.unescape(library['Library_Name__c']).replace("'","\\'")
  27.             image = charter["attachment1"]
  28.             stewname = html.unescape(library['Primary_Steward_s_Name__c']).replace("'","\\'")
  29.             email = html.unescape(library['Primary_Steward_s_Email__c'])
  30.             num = library['Official_Charter_Number__c']
  31.             story = html.unescape(library['Library_Story__c']).replace("'","\\'")
  32.             emailonmap = library['Email_on_Map__c']
  33.             nameonmap = library['Name_on_Map__c']
  34.             loc = library['Library_Geolocation__c']
  35.             lat = loc['latitude']
  36.             lon = loc['longitude']
  37.             street = html.unescape(library['Street__c']).replace("'","\\'")
  38.             city = html.unescape(library['City__c']).replace("'","\\'")
  39.             state = html.unescape(library['State_Province_Region__c']).replace("'","\\'")
  40.             country = html.unescape(library['Country__c']).replace("'","\\'")
  41.             zipcode = library['Postal_Zip_Code__c']
  42.             cid = library['Id']
  43.            
  44.             valid += 1
  45.         except:
  46.             skipped += 1
  47.             continue
  48.         query = f"insert into Charters values (e'{libname}', '{image}', e'{stewname}', '{email}', '{num}', e'{story}', {emailonmap}, {nameonmap}, {lat}, {lon}, e'{street}', e'{city}', e'{state}', e'{country}', '{zipcode}', '{cid}');"
  49.         cur.execute(query)
  50.     print(f'valid = {valid}, skipped = {skipped}')
  51.  
  52. # conn.commit()
  53. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement