sheet.nrows has a wrong value - python excel file
Solution 1
After trying some other files I'm sure it's about the file, and I think it's related to Microsoft 2003 and 2007 differences.
Solution 2
To make sure your file is not corrupt, try with another file; I doubt xlrd is buggy.
Also, I've cleaned up your code to look a bit nicer. For example the if row > 0
check is unneeded because you can just iterate over range(1, sheet.nrows)
in the first place.
def get_data_form_excel(address):
# this returns a generator not a list; you can iterate over it as normal,
# but if you need a list, convert the return value to one using list()
for sheet in xlrd.open_workbook(address).sheets():
for row in range(1, sheet.nrows):
yield [str(sheet.cell(row, col).value) for col in range(sheet.ncols)]
or
def get_data_form_excel(address):
# you can make this function also use a (lazily evaluated) generator instead
# of a list by changing the brackets to normal parentheses.
return [
[str(sheet.cell(row, col).value) for col in range(sheet.ncols)]
for sheet in xlrd.open_workbook(address).sheets()
for row in range(1, sheet.nrows)
]
Related videos on Youtube
Zeinab Abbasimazar
Looking to attain a challenging and responsible position as a software engineer and software analyst in telecommunication and software industry which effectively utilizes my personal, professional and educational skills and experiences. I’m also looking forward to learn and experience more on big data concepts/solutions.
Updated on September 16, 2022Comments
-
Zeinab Abbasimazar over 1 year
I got a really strange problem. I'm trying to read some data from an excel file, but the property
nrows
has a wrong value. Although my file has a lot of rows, it just returns 2.I'm working in pydev eclipse. I don't know what is actually the problem; everything looks fine.
When I try to access other rows by index manually, but I got the index error.
I appreciate any help.
If it helps, it's my code:
def get_data_form_excel(address): wb = xlrd.open_workbook(address) profile_data_list = [] for s in wb.sheets(): for row in range(s.nrows): if row > 0: values = [] for column in range(s.ncols): values.append(str(s.cell(row, column).value)) profile_data_list.append(values) print str(profile_data_list) return profile_data_list
-
Erik Kaplun over 10 years@ChrisProsser: my thought exactly: the input file might be corrupt.
-
-
zzxwill over 6 yearsIn which platform did you execute your code, macOS or Linux?