Python Forum

Full Version: Trouble reading files using pyexcel
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was able to fix the issue, I realized I accidentally type casted a variable! In terms of my other question though: should I use the
with open(...)
or would that not be appropriate in this scenario?
------------------
Hi,

I'm having some trouble using the pyexcel library to read .xlsx files. I already installed the package to read .xlsx files through pip3 but whenever I try running printing out the values in a for loop, or anywhere for that matter, it keeps giving me an error. I've tried using other inbuilt methods such as .keys(), .values(), etc. but they all keep giving me errors as well. My other question is: would it be good practice to put my code in
with open('test.xlsx', mode='r'.....)
would it make sense to do that in this situation and if so how would I incorporate that properly?

I will leave my code and my error down below. If anyone could help me that would be gladly appreciated! :)

Here is the code:
import pyexcel as p

records = p.get_records(file_name="test.xlsx")

for row in records:
	print(f"{row['Cells']}")
The error:
Error:
print(f"{row['Cells']}") TypeError: string indices must be integers
[EDIT] I just see that you are reffering to a row, not column. Is that what you want?

I don't have any experience with pyexcel. But have you tried to use index numbers instead of column names? The error message tells you need to use integers, so I assume the indexnumber of the column should work (typically starting from 0).
(Feb-06-2021, 07:45 PM)Jeff900 Wrote: [ -> ][EDIT] I just see that you are reffering to a row, not column. Is that what you want?

I don't have any experience with pyexcel. But have you tried to use index numbers instead of column names? The error message tells you need to use integers, so I assume the indexnumber of the column should work (typically starting from 0).

Thank you for the suggestions, I was able to solve it as I noticed that I accidentally typecasted one of the variables!