0

How can I read an excel file in python by importing csv? How can I read the columns and the rows? For instance I want to write a piece of code, which classifies a certain column if its value is greater than a determined number as accepted and otherwise (if less than that number) not accepted. I do not want to read the file in reverse order.

  • Possible duplicate of [How to read a CSV file in reverse order in Python?](https://stackoverflow.com/questions/10933838/how-to-read-a-csv-file-in-reverse-order-in-python) – dpapadopoulos Feb 24 '19 at 10:26
  • I don't intend to read a CSV file in reverse order. –  Feb 24 '19 at 10:27
  • You can't. You need a different module that can read excel files such as `pyxl`. If instead, you are talking about a `.csv` file, the fact that it can be opened by Excel doesn't change the fact that the format is not associated with Excel itself. What is the file extension? – roganjosh Feb 24 '19 at 10:27
  • Do I need to install a certain package? –  Feb 24 '19 at 10:29
  • You can use `win32com` or `xlwings` to direct Excel from Python and get Excel to it that way for you. – BoarGules Feb 24 '19 at 10:30
  • Possible duplicate of [Read in .xlsx with csv module in python](https://stackoverflow.com/questions/35744613/read-in-xlsx-with-csv-module-in-python) – Partho63 Feb 24 '19 at 10:31
  • 2
    Possible duplicate of [Read in .xlsx with csv module in python](https://stackoverflow.com/questions/35744613/read-in-xlsx-with-csv-module-in-python) – roganjosh Feb 24 '19 at 10:33

1 Answers1

0

Use the csv package. Check out the documentation here: https://docs.python.org/2/library/csv.html.

Specifically for your issue, I'd do something like this:

import csv

with open(myfile) as fp:
    rows = list(csv.reader(fp))

for row in rows:
    # let's grab all columns beyond the fifth
    for column in row[5:]:
        # do stuff

Personally, I like to label my columns. I'd recommend exploring csv.DictReader so that you can access your data by label, instead relying on arbitrary column headers, which are kind of arbitrary.

Just make sure to export your Excel file as a csv.

  • You cannot use the csv module to read Excel files. The last line of your answer is crucial; what happens if I have a folder of 1000 Excel files and want to automate reading them? Do I have to manually open each one and re-save? There are existing libraries that can do this for you. – roganjosh Feb 24 '19 at 10:39