Advertisement
Guest User

Untitled

a guest
Jan 31st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. # Read a CSV file in Python, libe-by-line, by Jeff Heaton (http://www.jeffheaton.com/tutorials/)
  2. import codecs
  3. import csv
  4. import os
  5. import psycopg2
  6. import sys
  7.  
  8. csvItems = os.listdir('../../data/CSV')
  9.  
  10. # FILENAME = "../../data/CSV/0valtice.csv"
  11. ENCODING = 'windows-1250'
  12.  
  13. conn = psycopg2.connect(host="localhost",database="uzemi", user="postgres", password="heslo")
  14. cur = conn.cursor()
  15. hold = 0
  16.  
  17. cur.execute('SELECT * FROM cities_csv_names WHERE NOT finished_simple;')
  18. csvNames = cur.fetchall()
  19.  
  20. print(len(csvNames))
  21.  
  22. for ar in csvNames:
  23. hold = hold + 1
  24. # print(ar)
  25.  
  26. FILENAME = ar[1]
  27. print('Filename: ', FILENAME)
  28.  
  29. curId = ar[0]
  30. print('curId: ', curId)
  31.  
  32. # quit()
  33. # continue
  34. with codecs.open('../../data/CSV/' + FILENAME, "r", ENCODING) as fp:
  35. reader = csv.reader(fp)
  36. # read CSV headers
  37. headers = next(reader)
  38.  
  39. # for loggin out name, on second row set to true and not logging anymore
  40. secondHold = False
  41.  
  42. # read rest of file
  43. for row in reader:
  44. # split each row for accessibility
  45. firstHoldRow = row[0].split(';')
  46.  
  47. # rows split
  48. # print(holdRow[0], holdRow[1], holdRow[2], holdRow[3], holdRow[4], holdRow[5], holdRow[6], holdRow[7], holdRow[8], holdRow[9], holdRow[10], holdRow[11], holdRow[12], holdRow[13], holdRow[14], holdRow[15], holdRow[16], holdRow[17], holdRow[18])
  49. holdRow = []
  50.  
  51. # empty strings set to null because of db
  52.  
  53. for x in range(len(firstHoldRow)):
  54. if not firstHoldRow[x]:
  55. holdRow.append(None)
  56. else:
  57. holdRow.append(firstHoldRow[x])
  58.  
  59.  
  60. # print(holdRow)
  61. # quit()
  62. # just logging the name
  63. if secondHold == False:
  64. print('City: ', holdRow[2])
  65. secondHold = True
  66.  
  67.  
  68. cityId = holdRow[1]
  69. cityName = holdRow[2]
  70. cityPartId = holdRow[7]
  71. cityPartName = holdRow[8]
  72. cityStreetId = holdRow[9]
  73. cityStreetName = holdRow[10]
  74. soType = holdRow[11]
  75. numberDomovni = holdRow[12]
  76. numberOrientacni = holdRow[13]
  77. signOrientacni = holdRow[14]
  78. psc = holdRow[15]
  79. xCoord = holdRow[17]
  80. yCoord = holdRow[16]
  81.  
  82. # NEW TABLE cities_data_simple
  83. sql = """ INSERT INTO cities_data_simple
  84. (
  85. city_id,
  86. city_name,
  87. city_part_id,
  88. city_part_name,
  89. city_street_id,
  90. city_street_name,
  91. so_type,
  92. number_domovni,
  93. number_orientacni,
  94. sign_orientacni,
  95. psc,
  96. x_coord,
  97. y_coord,
  98. names_id)
  99. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
  100.  
  101. cur.execute(sql, (
  102. cityId,
  103. cityName,
  104. cityPartId,
  105. cityPartName,
  106. cityStreetId,
  107. cityStreetName,
  108. soType,
  109. numberDomovni,
  110. numberOrientacni,
  111. signOrientacni,
  112. psc,
  113. xCoord,
  114. yCoord,
  115. curId
  116. ))
  117.  
  118.  
  119. # OLD TABLE cities_data
  120. # sql = """INSERT INTO cities_data
  121. # (
  122. # kod_adm,
  123. # kod_obce,
  124. # nazev_obce,
  125. # kod_momc,
  126. # nazev_momc,
  127. # kod_mop,
  128. # nazev_mop,
  129. # kod_casti_obce,
  130. # nazev_casti_obce,
  131. # kod_ulice,
  132. # nazev_ulice,
  133. # typ_so,
  134. # cislo_domovni,
  135. # cislo_orientacni,
  136. # znak_cisla_orientacniho,
  137. # psc,
  138. # souradnice_y,
  139. # souradnice_x,
  140. # platnost_od,
  141. # names_id)
  142. # VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""".replace('\n', ' ')
  143.  
  144. # cur.execute(sql, (holdRow[0], holdRow[1], holdRow[2], holdRow[3], holdRow[4], holdRow[5], holdRow[6], holdRow[7], holdRow[8], holdRow[9], holdRow[10], holdRow[11], holdRow[12], holdRow[13], holdRow[14], holdRow[15], holdRow[16], holdRow[17], holdRow[18], curId))
  145.  
  146. cur.execute('UPDATE cities_csv_names SET finished_simple = TRUE WHERE index = %s;', [curId])
  147.  
  148. print('Committing. ', hold, ' / ', len(csvNames))
  149. print('*************')
  150. conn.commit()
  151. # quit()
  152.  
  153. # Print individual fields of the row
  154. # print("{},{},{},{} = {}".format(row[0],row[1],row[2],row[3],row[4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement