Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import xlrd
  2. import os
  3. import pandas as pd
  4.  
  5.  
  6. def get_daily_data(sheet_name):
  7. attributes = {}
  8. try:
  9. book = xlrd.open_workbook(sheet_name)
  10. sheet = book.sheet_by_name('Sheet1')
  11. # static variables
  12. try:
  13. attributes['net_sales'] = round(float(sheet.cell(9, 14).__str__()[7:]))
  14. attributes['breakfast_sales'] = round(float(sheet.cell(25, 13).__str__()[7:]))
  15. attributes['lunch_sales'] = round(float(sheet.cell(24, 13).__str__()[7:]))
  16. attributes['dinner_sales'] = round(float(sheet.cell(23, 13).__str__()[7:]))
  17. attributes['latenight_sales'] = round(float(sheet.cell(21, 13).__str__()[7:]))
  18.  
  19. attributes['total_customers'] = sheet.cell(27, 19).__str__()[7:]
  20. attributes['breakfast_customers'] = sheet.cell(25, 23).__str__()[7:]
  21. attributes['lunch_customers'] = sheet.cell(24, 23).__str__()[7:]
  22. attributes['dinner_customers'] = sheet.cell(23, 23).__str__()[7:]
  23. attributes['latenight_customers'] = sheet.cell(21, 23).__str__()[7:]
  24. except ValueError:
  25. print(sheet_name + ' is missing at least one meal.')
  26. pass
  27.  
  28. # dynamic variables
  29. for row in range(37, 77):
  30. cell = sheet.cell(row, 26).__str__()
  31. if cell != "empty:''":
  32. attributes[str(cell[5:].lower().replace("'", '')) + '_sold'] = int(float(sheet.cell(row, 32).__str__()[7:]))
  33. if cell == "text:'Snacks'":
  34. attributes[str(cell[5:].lower().replace("'", '')) + '_sold'] = int(float(sheet.cell(row, 32).__str__()[7:]))
  35. break
  36. return attributes
  37. except xlrd.biffh.XLRDError:
  38. print(sheet_name + ' failed to process correctly.')
  39. pass
  40.  
  41.  
  42. # store all data
  43. data_dict = {}
  44. for filename in os.listdir(r'/Users/CarterW.Ward/Desktop/DOTSWeather/venv/kkd'):
  45. # string repair to match master keys
  46. full_date = filename[:10]
  47. day = full_date[3:5]
  48. month = full_date[0:2]
  49. year = full_date[6:]
  50. corrected_date = year + '-' + month + '-' + day
  51. data_dict[corrected_date] = get_daily_data(r'/Users/CarterW.Ward/Desktop/DOTSWeather/venv/kkd/' + filename)
  52. max_len = 0
  53. max_key = ''
  54. trash = []
  55. for date in data_dict: # finds longest set of attributes
  56. if data_dict[date] is None:
  57. trash.append(date)
  58. else:
  59. cur_len = len(data_dict[date])
  60. if cur_len > max_len:
  61. max_key = date
  62. max_len = cur_len
  63.  
  64. for piece_of in trash:
  65. data_dict.pop(piece_of)
  66. print(data_dict)
  67. names = list(data_dict[max_key].keys())
  68. df = pd.DataFrame(columns=names) # sets longest list of attributes to column names
  69.  
  70. # add date and write to excel
  71. for day in data_dict.keys():
  72. data_dict[day]['Date'] = day
  73. df = df.append(data_dict[day], ignore_index=True)
  74. df = df.set_index('Date')
  75. df.to_excel('store_data.xlsx')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement