Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. def insert_into_table(data):
  2. # print json.dumps(data, indent=4)
  3.  
  4. conn = psycopg2.connect(database='testdb', user='postgres', password='password', host='localhost')
  5. cursor = conn.cursor()
  6. for i in data:
  7. # print json.dumps(i, indent=4)
  8. iso_code = i["iso_code"]
  9.  
  10. if i["l_postcode"] is None:
  11. l_postcode = 'NULL'
  12. else:
  13. l_postcode = i["l_postcode"]
  14.  
  15. if i["r_postcode"] is None:
  16. r_postcode = 'NULL'
  17. else:
  18. r_postcode = i["r_postcode"]
  19. link_id = i["link_id"]
  20. geom = i["geometry"]
  21.  
  22. # sql = 'INSERT into data_load VALUES ('{}', {}, {},{} , st_geomfromgeojson('{}'))'.format(iso_code, l_postcode, r_postcode, link_id, json.dumps(geom))
  23. sql = """INSERT into data_load (iso_code, l_postcode, r_postcode, link_id, geom) VALUES ('{}',{},{},{},st_geomfromgeojson('{}'));""".format(iso_code, l_postcode, r_postcode, link_id, json.dumps(geom))
  24. print sql
  25.  
  26. cursor.execute(sql, )
  27. print sql
  28.  
  29. cursor.close()
  30. conn.commit()
  31.  
  32. conn.close()
  33.  
  34. [
  35. {
  36. "geometry": {
  37. "type": "LineString",
  38. "coordinates": [
  39. [
  40. -91.98979,
  41. 15.644559999999998
  42. ],
  43. [
  44. -91.98971,
  45. 15.645249999999999
  46. ]
  47. ]
  48. },
  49. "iso_code": "MEX",
  50. "l_postcode": null,
  51. "r_postcode": null,
  52. "link_id": 1186786776
  53. },
  54. {
  55. "geometry": {
  56. "type": "LineString",
  57. "coordinates": [
  58. [
  59. -106.77742,
  60. 28.390159999999998
  61. ],
  62. [
  63. -106.77806,
  64. 28.39076
  65. ]
  66. ]
  67. },
  68. "iso_code": "MEX",
  69. "l_postcode": null,
  70. "r_postcode": null,
  71. "link_id": 818231403
  72. },
  73. {
  74. "geometry": {
  75. "type": "LineString",
  76. "coordinates": [
  77. [
  78. -98.89940999999999,
  79. 18.90605
  80. ],
  81. [
  82. -98.89926,
  83. 18.906689999999998
  84. ]
  85. ]
  86. },
  87. "iso_code": "MEX",
  88. "l_postcode": null,
  89. "r_postcode": null,
  90. "link_id": 1130886811
  91. }
  92. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement