0

I have this working:

address = []

for row in ws.iter_rows(row_offset=4,column_offset=3):
    if len(row) > 0:
        cell = row[2]
        if cell.internal_value is not None: 
           *something here, append appropriate cell values to address*

print address
return address

Which will successfully go through a column in an excel spreadsheet and get every nonempty cell. At the asterisks, though, I am unsure how to go through the output and pull out only the numbers. I know that might be odd to read through, but the numbers I need are the "1.0, 2.0, 3.0, 4.0, etc". All of the other parts, and the actual numbers themselves, are subject to change as I read in different spreadsheets, but they will be in the same format.

So what I need to know is how i would go through this list and get rid of everything that isnt a decimal number, and ideally convert those decimals into integers.

Im also sure that there is a good way to do this with list comprehension, but I've only been programmning in python for a week so Im not sure how to really do that.

Sorry if this is confusing, and thanks for any help in advance.

zakparks31191
  • 919
  • 2
  • 21
  • 42

4 Answers4

1

There's no excellent way of checking if a string is a float in Python, but this should suffice.

>>> def is_float(n):
...     try:
...         float(n)
...         return True
...     except ValueError:
...         return False
>>> x = ["garbage", "1.0", "trash", "2.0"]
>>> [i for i in x if is_float(i)]
['1.0', '2.0']
Community
  • 1
  • 1
tsm
  • 3,598
  • 2
  • 21
  • 35
1

You can do this:

int_values = [int(v) for v in cell.internal_values if type(v) == float]

Or, I noticed that these values are separated by one item, so you might want this too:

int_values = [int(cell.internal_values[i]) for i in xrange(1, len(cell.internal_values), 2)]

(you might need to use range(1, ...) because the first one should be discarded, the pattern starts at index 1).

jadkik94
  • 7,000
  • 2
  • 30
  • 39
  • i'll try the first. As i said in the original post, the pattern is arbitrary, and may (read: will) change every time, so im not sure getting that specific will be necessary :) – zakparks31191 Jun 22 '12 at 15:45
  • Use `isinstance(v,float)` instead of checking `type`. – jamylak Jun 23 '12 at 00:45
0

So what I need to know is how i would go through this list and get rid of everything that isnt a decimal number, and ideally convert those decimals into integers.

Since your list contains floats and not strings that are floats:

only_floats = [i for i in big_list if isinstance(float,i)]
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

I'm adding this answer as a "part one" in case anyone in the future needs the solution to the first part. The second part of the question (converting list of float to int) is in the accepted answer.

To rip out all the floats, the original algorithm was revised to this:

addr = []

for row in ws.iter_rows(row_offset=4,column_offset=3):
    if row:
        cell = row[2]
        try:
            addr.append(float(cell.internal_value))
        except ValueError:
            pass
        except TypeError:
            pass

address = [int(v) for v in addr if type(v) == float]

The last line is the code from the accepted answer. The algorithm revision was actually the work of someone else on here, but the post was deleted! So thank you mystery user, I wish i could credit you here.

zakparks31191
  • 919
  • 2
  • 21
  • 42