0

I'm importing data with XLRD. The overall project involves pulling data from existing Excel files, but there are merged cells. Essentially, an operator is accounting for time on one of 3 shifts. As such, for each date on the grid they're working with, there are 3 columns (one for each shift). I want to change the UI as little as possible. As such, when I pull the list of the dates covered on the sheet, I want it to have the same date 3 times in a row, once for each shift.

Here's what I get:

[41665.0, '', '', 41666.0, '', '', 41667.0, '', '', 41668.0, '', '', 41669.0, '', '', 41670.0, '', '', 41671.0, '', '']

Here's what I want:

[41665.0, 41665.0, 41665.0, 41666.0, 41666.0, 41666.0, 41667.0, 41667.0, 41667.0, 41668.0, 41668.0, 41668.0, 41669.0, 41669.0, 41669.0, 41670.0, 41670.0, 41670.0, 41671.0, 41671.0, 41671.0]

I got this list with:

dates = temp_sheet.row_values(2)[2:]

I don't know if the dates will always show up as floats. I was thinking something along the lines of:

dates = [i for i in dates if not i.isspace() else j]

It throws an error when it gets to the first value, which is a float. .isnumeric() is a string operator...there has to be a much more elegant way. I found this answer but it seems much more involved than what I believe I need.

Community
  • 1
  • 1
mauve
  • 2,707
  • 1
  • 20
  • 34

2 Answers2

1

I dont think this is a good application for a comprehension, and an explicit loop would be better:

lst = [41665.0, '', '', 41666.0, '', '', 41667.0, '', '', 41668.0, '', '', 41669.0, '', '', 41670.0, '', >>> lst = [41665.0, '', '', 41666.0, '', '', 41667.0, '', '', 41668.0, '', '', 41669.0, '', '', 41670.0, '', '', 41671.0, '', '']
>>> temp = lst[0]
>>> nlst = []
>>> for i in lst:
...     if i: temp = i
...
...     nlst.append(temp)
...
>>> nlst
[41665.0, 41665.0, 41665.0, 41666.0, 41666.0, 41666.0, 41667.0, 41667.0, 41667.0, 41668.0, 41668.0, 41668.0, 41669.0, 41669.0, 41669.0, 41670.0, 41670.0, 41670.0, 41671.0, 41671.0, 41671.0]
wnnmaw
  • 5,444
  • 3
  • 38
  • 63
0

Another solution,

>>> l = [41665.0, '', '', 41666.0, '', '', 41667.0, '', '', 41668.0, '', '', 41669.0, '', '', 41670.0, '', '', 41671.0, '', '']
>>> lst = [ item for item in l if item is not '']
>>> [ v for item in zip(lst,lst,lst) for v in item ]
[41665.0, 41665.0, 41665.0, 41666.0, 41666.0, 41666.0, 41667.0, 41667.0, 41667.0, 41668.0, 41668.0, 41668.0, 41669.0, 41669.0, 41669.0, 41670.0, 41670.0, 41670.0, 41671.0, 41671.0, 41671.0]
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42