简体   繁体   中英

How to read a cell in excel using a python for loop?

I am making an excel file reading program in python. I want it to read each cell in a particular column. I used a for loop for this. This is my code:

location = "C:\\Users\\user2\\Desktop\\Python\\Other\\blah.xls"
workbook = xlrd.open_workbook(location)
sheet = workbook.sheet_by_index(0)
rownum = 1
for word in (sheet.cell_value(rownum,2)):
    print(word)
    rownum += 1

And I get the result:

R
E
S
T
A
R
T

I
S
S
U
E

/

R
A
M

P
R
O
B
L
E
M

Instead, I want the result to show word by word, not letter by letter.

Instead of

for word in (sheet.cell_value(rownum,2)):

Use

for word in (sheet.cell_value(rownum,2).split(' ')):

If you use for on a string, it will iterate over single characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM