Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. import mysql.connector
  2. import json
  3. from pprint import pprint
  4.  
  5. print('Antes Mysql')
  6.  
  7. cnx = mysql.connector.connect(user='root', password='7qt5wzs6',
  8. host='mypolitician02',
  9. database='meupolitico')
  10.  
  11.  
  12. cursor = cnx.cursor()
  13.  
  14. print('Antes abrir arquivo')
  15. with open('/opt/ceap/Ano-2016.json', encoding='latin-1') as data_file:
  16. print('depois withopen')
  17. #data = json.load(data_file)
  18. #data = json.JSONDecoder(data)
  19. #print(data)
  20. #data = data["DESPESA"]
  21. #data = exemple
  22. #pprint(data)
  23. #i=0
  24. #for key in data["DESPESA"]:
  25. # result = key
  26. # i=i+1
  27. # print(i)
  28.  
  29. print('json.load')
  30. try:
  31. jsonToPython = json.load(data_file)
  32. except:
  33. print('Error: ', sys.exc_info()[0])
  34.  
  35. pprint('Leu o arquivo')
  36.  
  37. txNomeParlamentar = ""
  38. vlrLiquido = ""
  39. nuCarteiraParlamentar = ""
  40. numMes = ""
  41. nuLegislatura = ""
  42. numEspecificacaoSubCota = ""
  43. codLegislatura = ""
  44. nuDeputadoId = ""
  45. ideDocumento = ""
  46. ideCadastro = ""
  47. txtTrecho = ""
  48. txtDescricao = ""
  49. sgUF = ""
  50. txtDescricaoEspecificacao = ""
  51. numAno = ""
  52. txtNumero = ""
  53. numRessarcimento = ""
  54. indTipoDocumento = ""
  55. txtCNPJCPF = ""
  56. numSubCota = ""
  57. vlrGlosa = ""
  58. txtFornecedor = ""
  59. sgPartido = ""
  60. vlrDocumento = ""
  61. numLote = ""
  62. vlrRestituicao = ""
  63. datEmissao = ""
  64. numParcela = ""
  65. txtPassageiro = ""
  66.  
  67. #################################################
  68. #exibindo item com value e key
  69. i=0
  70. for item in jsonToPython['DESPESA']:
  71. i=i+1
  72. print(i)
  73.  
  74. for key,value in item.items():
  75. #pprint("--key--["+key+"]"+"--value--["+value+"]")
  76. if key == "txNomeParlamentar":
  77. if value is None:
  78. value = ""
  79. txNomeParlamentar = value
  80. elif key == "vlrLiquido":
  81. if value is None:
  82. value = 0
  83. value = value.replace(',','.')
  84. vlrLiquido = float(value)
  85. elif key == "nuCarteiraParlamentar":
  86. if value is None:
  87. value = 0
  88. nuCarteiraParlamentar = int(value)
  89. elif key == "numMes":
  90. if value is None:
  91. value = 0
  92. numMes = int(value)
  93. elif key == "nuLegislatura":
  94. if value is None:
  95. value = 0
  96. nuLegislatura = int(value)
  97. elif key == "numEspecificacaoSubCota":
  98. if value is None:
  99. value = 0
  100. numEspecificacaoSubCota = int(value)
  101. elif key == "codLegislatura":
  102. if value is None:
  103. value = 0
  104. codLegislatura = int(value)
  105. elif key == "nuDeputadoId":
  106. if value is None:
  107. value = 0
  108. nuDeputadoId = int(value)
  109. elif key == "ideDocumento":
  110. if value is None:
  111. value = 0
  112. if value == "":
  113. value = 0
  114. ideDocumento = int(value)
  115. elif key == "ideCadastro":
  116. if value is None:
  117. value = 0
  118. if value == "":
  119. value = 0
  120. print(value)
  121. ideCadastro = int(value)
  122. elif key == "txtTrecho":
  123. if value is None:
  124. value = ""
  125. txtTrecho = value
  126. elif key == "txtDescricao":
  127. if value is None:
  128. value = ""
  129. txtDescricao = value
  130. elif key == "sgUF":
  131. if value is None:
  132. value = ""
  133. sgUF = value
  134. elif key == "txtDescricaoEspecificacao":
  135. if value is None:
  136. value = ""
  137. txtDescricaoEspecificacao = value
  138. elif key == "numAno":
  139. if value is None:
  140. value = 0
  141. numAno = int(value)
  142. elif key == "txtNumero":
  143. if value is None:
  144. value = ""
  145. txtNumero = value
  146. elif key == "numRessarcimento":
  147. if value is None:
  148. value = 0
  149. numRessarcimento = int(value)
  150. elif key == "indTipoDocumento":
  151. if value is None:
  152. value = 0
  153. indTipoDocumento = int(value)
  154. elif key == "txtCNPJCPF":
  155. if value is None:
  156. value = ""
  157. txtCNPJCPF = value
  158. elif key == "numSubCota":
  159. if value is None:
  160. value = 0
  161. numSubCota = int(value)
  162. elif key == "vlrGlosa":
  163. if value is None:
  164. value = 0
  165. value = value.replace(',','.')
  166. vlrGlosa = float(value)
  167. elif key == "txtFornecedor":
  168. if value is None:
  169. value = ""
  170. txtFornecedor = value
  171. elif key == "sgPartido":
  172. if value is None:
  173. value = ""
  174. sgPartido = value
  175. elif key == "vlrDocumento":
  176. if value is None:
  177. value = 0
  178. value = value.replace(',','.')
  179. vlrDocumento = value
  180. elif key == "numLote":
  181. if value is None:
  182. value = 0
  183. numLote = int(value)
  184. elif key == "vlrRestituicao":
  185. if value is None:
  186. value = 0
  187. value = value.replace(',','.')
  188. vlrRestituicao = value
  189. elif key == "datEmissao":
  190. if value is None:
  191. value = ""
  192. datEmissao = value
  193. elif key == "numParcela":
  194. if value is None:
  195. value = 0
  196. numParcela = int(value)
  197. elif key == "txtPassageiro":
  198. if value is None:
  199. value = ""
  200. txtPassageiro = value
  201.  
  202. params = [txNomeParlamentar, vlrLiquido, nuCarteiraParlamentar, numMes, nuLegislatura, numEspecificacaoSubCota, codLegislatura,nuDeputadoId, ideDocumento, ideCadastro, txtTrecho, txtDescricao, sgUF, txtDescricaoEspecificacao, numAno, txtNumero, numRessarcimento, indTipoDocumento, txtCNPJCPF, numSubCota, vlrGlosa, txtFornecedor, sgPartido, vlrDocumento, numLote, vlrRestituicao, datEmissao, numParcela, txtPassageiro]
  203.  
  204. #pprint(params)
  205.  
  206. query = "INSERT INTO meupolitico.despesas (txNomeParlamentar, vlrLiquido, nuCarteiraParlamentar, numMes, nuLegislatura, numEspecificacaoSubCota, codLegislatura, nuDeputadoId, ideDocumento, ideCadastro, txtTrecho, txtDescricao, sgUF, txtDescricaoEspecificacao, numAno, txtNumero, numRessarcimento, indTipoDocumento, txtCNPJCPF, numSubCota, vlrGlosa, txtFornecedor, sgPartido, vlrDocumento, numLote, vlrRestituicao, datEmissao, numParcela, txtPassageiro) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
  207.  
  208. cursor.execute(query, params)
  209. cnx.commit()
  210.  
  211. #pprint(query)
  212.  
  213. #pprint("----------------------------------------")
  214. #pprint("-----------------query------------------")
  215. #pprint("----------------------------------------")
  216.  
  217. txNomeParlamentar = ""
  218. vlrLiquido = ""
  219. nuCarteiraParlamentar = ""
  220. numMes = ""
  221. nuLegislatura = ""
  222. numEspecificacaoSubCota = ""
  223. codLegislatura = ""
  224. nuDeputadoId = ""
  225. ideDocumento = ""
  226. ideCadastro = ""
  227. txtTrecho = ""
  228. txtDescricao = ""
  229. sgUF = ""
  230. txtDescricaoEspecificacao = ""
  231. numAno = ""
  232. txtNumero = ""
  233. numRessarcimento = ""
  234. indTipoDocumento = ""
  235. txtCNPJCPF = ""
  236. numSubCota = ""
  237. vlrGlosa = ""
  238. txtFornecedor = ""
  239. sgPartido = ""
  240. vlrDocumento = ""
  241. numLote = ""
  242. vlrRestituicao = ""
  243. datEmissao = ""
  244. numParcela = ""
  245. txtPassageiro = ""
  246.  
  247. cursor.close()
  248.  
  249. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement