0

I'm experiencing weird behavior when appending values to a python list. I am using openpyxl to read values of multiple excel files, do some math and write them to a new excel file. However when looping through the rows in a header column and appending the values to a list, the final result of the list is the value of the cell plus an 'L'. For example, here is the relevant pieces of my code:

xl_file = 'path/to/excel/file.xlsx'
wb = openpyxl.load_workbook(xl_file, data_only=True)
sheet = wb.get_sheet_by_name("sheetname")

hdr_col = [] # empty list to store contents of column header
for r in range(3, 13): # we want rows 3-12. rows 1 and 2 of column hdr are empty
    val = sheet.cell(row=r, column = 1).value
    print val # prints the actual value (1, 2, 3,..., 10)
    hdr_col.append(val)
print hdr_col # prints values with L, i.e.:

[1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L]

Can someone explain to me what's going on? At first I thought it was the openpyxl module but the L only gets added after appending the value to a list so I'm now thinking it's actually Python.

user20408
  • 679
  • 2
  • 7
  • 23
  • Why does this matter, specifically? – Daniel Roseman Jul 25 '16 at 18:11
  • @DanielRoseman it doesn't, I was just curious. I found out it just means the values are type Long from looping thru the list and printing out the values and their type. And the person below answered my question as to why they were printing that way. – user20408 Jul 25 '16 at 18:16

1 Answers1

1

Because print returns human readable format while if you use repr in place of print at your statement print val you actually find 'L' appended to every integer

Check here

In [52]: a = 1l

In [53]: print a
1

In [54]: type(a)
Out[54]: long

In [58]: repr(a)
Out[58]: '1L'

In [55]: s = []

In [56]: s.append(a)

In [57]: s
Out[57]: [1L]

because mysql and other databases stores integer value as long.

Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
  • yes, after looping thru the list and printing the values I'm seeing that they don't actually print out that way and they are type long, so this makes sense. I think you also answered why it's doing it that way, so thanks. – user20408 Jul 25 '16 at 18:17
  • @user20408 if you find this answer helpful and correct please mark collect. – Vishnu Upadhyay Jul 25 '16 at 18:19
  • just did, I don't have enough points to accept it right away. thanks – user20408 Jul 25 '16 at 18:23