Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. import csv
  2. import matplotlib.pyplot as plt
  3.  
  4. rawdata = []
  5. with open('TCBT.csv', 'r', encoding='CP866') as file:
  6. r = csv.reader(file)
  7. for line in r:
  8. rawdata.append(line)
  9. # print(rawdata)
  10.  
  11. napravlenie = [x[0] for x in rawdata]
  12. period = [x[1] for x in rawdata]
  13. country = [x[2] for x in rawdata]
  14. tnved = [x[3] for x in rawdata]
  15. cost = [x[5] for x in rawdata]
  16. netto = [x[6] for x in rawdata]
  17.  
  18. index = []
  19. for i in range(len(tnved)):
  20. if tnved[i] == '2401':
  21. index.append(i)
  22. # print(index)
  23.  
  24. newindex = []
  25. for i in index:
  26. if napravlenie[i] == "ИМ":
  27. newindex.append(i)
  28. # print(newindex)
  29.  
  30. newcountry = []
  31. for i in newindex:
  32. newcountry.append(country[i])
  33. # print(newcountry)
  34.  
  35. newperiod = []
  36. for i in newindex:
  37. newperiod.append(period[i])
  38. # print(newperiod)
  39.  
  40. newnetto = []
  41. for i in newindex:
  42. newnetto.append(float(netto[i]) / 1000) # Количество ввозимого табака в тоннах
  43. # print(newnetto)
  44.  
  45. newcost = []
  46. for i in newindex:
  47. newcost.append(float(cost[i]) / 1000) # Стоимость табака в $ тыс.
  48. # print(newcost)
  49.  
  50. uniquecountry = []
  51. place = []
  52. places = []
  53. for i in newcountry:
  54. if i not in uniquecountry:
  55. uniquecountry.append(i)
  56. for j in range(len(uniquecountry)):
  57. for i in range(len(newcountry)):
  58. if uniquecountry[j] == newcountry[i]:
  59. place.append(i)
  60. places.append(place)
  61. place = []
  62. # print(uniquecountry)
  63. # print(places)
  64.  
  65. value = 0
  66. values = []
  67. for i in places:
  68. for j in i:
  69. value += newnetto[j]
  70. values.append(value)
  71. value = 0
  72. # print(values)
  73.  
  74. dic = {values[i]: uniquecountry[i] for i in range(len(uniquecountry))}
  75. # print(dic)
  76. values.sort()
  77. largest_import = values[-10:]
  78. largest_dealers = []
  79. for i in largest_import:
  80. largest_dealers.append(dic[i])
  81. # print(largest_dealers)
  82.  
  83. # Запись рейтинга в CSV файл
  84. iposition=[]
  85. with open("Рейтинг.csv", "a", newline = '') as file:
  86. for i in values:
  87. iposition.append(i)
  88. iposition.append(dic[i])
  89. writer = csv.writer(file)
  90. writer.writerow(iposition)
  91. iposition = []
  92.  
  93. value2 = 0
  94. values2 = []
  95. for i in places:
  96. for j in i:
  97. value2 += newcost[j]
  98. values2.append(value2)
  99. value2 = 0
  100. # print(values2)
  101.  
  102. dic2 = {values2[i]: uniquecountry[i] for i in range(len(uniquecountry))}
  103. # print(dic2)
  104. values2.sort()
  105. expensive_cost = values2[-10:]
  106. expensive_dealers = []
  107. for i in expensive_cost:
  108. expensive_dealers.append(dic2[i])
  109.  
  110. periodBR = []
  111. costBR = []
  112. nettoBR = []
  113. for i in range(len(newcountry)):
  114. if newcountry[i] == 'BR':
  115. periodBR.append(newperiod[i])
  116. costBR.append(newcost[i])
  117. nettoBR.append(newnetto[i])
  118. cost2013 = netto2013 = cost2014 = netto2014 = cost2015 = netto2015 = cost2016 = netto2016 = 0
  119. for i in range(len(periodBR)):
  120. if periodBR[i] == '2013':
  121. cost2013 += costBR[i]
  122. netto2013 += nettoBR[i]
  123. elif periodBR[i] == '2014':
  124. cost2014 += costBR[i]
  125. netto2014 += nettoBR[i]
  126. elif periodBR[i] == '2015':
  127. cost2015 += costBR[i]
  128. netto2015 += nettoBR[i]
  129. elif periodBR[i] == '2016':
  130. cost2016 += costBR[i]
  131. netto2016 += nettoBR[i]
  132.  
  133. fig = plt.figure()
  134. plt.bar(largest_dealers, largest_import)
  135. plt.title('Крупнейшие импортеры табака в Россию')
  136. plt.xlabel('Страны')
  137. plt.ylabel('Количество табака, т')
  138. plt.grid(True)
  139. # plt.show()
  140.  
  141. fig = plt.figure()
  142. plt.bar(expensive_dealers, expensive_cost, color = 'red')
  143. plt.title('Крупнейшие импортеры табака в Россию')
  144. plt.xlabel('Страны')
  145. plt.ylabel('Стоимость, $ тыс.')
  146. plt.grid(True)
  147. # plt.show()
  148.  
  149. fig = plt.figure()
  150. years = ['2013', '2014', '2015', '2016']
  151. cost_on_years = [cost2013, cost2014, cost2015, cost2016]
  152. netto_on_years = [netto2013, netto2014, netto2015, netto2016]
  153. plt.title('Импорт из Бразилии по годам')
  154. plt.bar([x + 0.05 for x in range(len(years))], cost_on_years,
  155. width = 0.2, color = 'red', alpha = 0.7, label = 'Стоимость табака, $ тыс.',
  156. zorder = 2)
  157. plt.bar([x + 0.3 for x in range(len(years))], netto_on_years,
  158. width = 0.2, color = 'blue', alpha = 0.7, label = 'Количество табака, т',
  159. zorder = 2)
  160. plt.legend(loc = 'upper right')
  161. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement