How to Read Lines From Excel in Python
When you write a program in Python, you might at some point need to import data from a type of file -- like Excel -- into the Python program for processing. You can perform this data input using Python's built-in file reading functions to load the Excel file into memory and then read each row into memory, along with the associated column data. This information can then be passed along to other parts of your program for further use.
Instructions
-
-
1
Open the Python program on which you are working.
-
2
Type "excelFile = open('/path/to/excel/file.xls', 'r')" on a new line in your program to initialize a new variable (excelFile) that reads in the Excel file from its location (/path/to/excel/file.xls) on your computer.
-
-
3
Press "Enter" to create a new line.
-
4
Type "for line in excelFile: print line," to create a loop that will go through each set of rows in the Excel file and print them to the screen.
-
1
Tips & Warnings
You can substitute other file operations for the "print" operation in Step 4 to do things like save the data to another file, delete it from the Excel file or manipulate it and then save it back to the file. The print operation is given as an example.