Advertisement
homeworkhelp111

SqlDataset

Apr 12th, 2023 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. import csv
  2. import datetime
  3.  
  4. def main():
  5. file = open("sample.csv")
  6. csvreader = csv.reader(file)
  7. header = next(csvreader)
  8. rows = []
  9.  
  10. shops = {}
  11.  
  12. for row in csvreader:
  13. rows.append(row)
  14. shop_id = int(row[0])
  15. if(shop_id not in shops):
  16. shops[shop_id] = [datetime.datetime(int(row[1][0:4]), int(row[1][5:7]),int(row[1][8:]))]
  17. else:
  18. shops[shop_id].append(datetime.datetime(int(row[1][0:4]), int(row[1][5:7]),int(row[1][8:])))
  19.  
  20.  
  21. closed = {}
  22. opened = {}
  23.  
  24. for shop in shops:
  25. shops[shop].sort()
  26. i = 0
  27. opendate = datetime.datetime(2021,1,1)
  28. while i < len(shops[shop]) - 1:
  29. if((shops[shop][i+1] - shops[shop][i]).days > 30):
  30. if(shop not in closed):
  31. opened[shop] = [[opendate, shops[shop][i]]]
  32. closed[shop] = [[shops[shop][i] + datetime.timedelta(days=1), shops[shop][i+1] - datetime.timedelta(days=1)]]
  33. opendate = shops[shop][i+1]
  34. else:
  35. opened[shop].append([opendate, shops[shop][i]])
  36. closed[shop].append([shops[shop][i] + datetime.timedelta(days=1), shops[shop][i+1] - datetime.timedelta(days=1)])
  37. opendate = shops[shop][i+1]
  38. i += 1
  39.  
  40. if((datetime.datetime(2022,12,31) - shops[shop][i]).days > 30):
  41. if(shop not in closed):
  42. opened[shop] = [[opendate, shops[shop][i]]]
  43. closed[shop] = [[shops[shop][i] + datetime.timedelta(days=1), "NULL"]]
  44. else:
  45. opened[shop].append([opendate, shops[shop][i]])
  46. closed[shop].append([shops[shop][i] + datetime.timedelta(days=1), "NULL"])
  47.  
  48. for shop in shops:
  49. if(shop not in closed):
  50. opened[shop] =[[datetime.datetime(2021,1,1),"NULL"]]
  51.  
  52. for shop in closed:
  53. if(closed[shop][-1][1]!="NULL"):
  54. opened[shop].append([closed[shop][-1][1] + datetime.timedelta(days=1),"NULL"])
  55.  
  56. with open('SqlDataset.csv','w',newline='') as f:
  57. writer = csv.writer(f)
  58. writer.writerow(['shop_id','status','lower_range','upper_range'])
  59. outputdata = []
  60. for shop in opened:
  61. for interval in opened[shop]:
  62. if(interval[1]!="NULL"):
  63. outputdata.append([shop, 'open', datetime.datetime.strftime(interval[0],"%Y-%m-%d"), datetime.datetime.strftime(interval[1],"%Y-%m-%d")])
  64. else:
  65. outputdata.append([shop, 'open', datetime.datetime.strftime(interval[0],"%Y-%m-%d"), "NULL"])
  66.  
  67. for shop in closed:
  68. for interval in closed[shop]:
  69. if(interval[1]!="NULL"):
  70. outputdata.append([shop, 'clsd', datetime.datetime.strftime(interval[0],"%Y-%m-%d"), datetime.datetime.strftime(interval[1],"%Y-%m-%d")])
  71. else:
  72. outputdata.append([shop, 'clsd', datetime.datetime.strftime(interval[0],"%Y-%m-%d"), "NULL"])
  73. outputdata.sort(key=lambda x:(x[0],x[2]))
  74. writer.writerows(outputdata)
  75.  
  76. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement