Search code examples
pythonpython-2.7mayaimport-from-excelpymel

Extract information from Excel with python 2.7/ pyMel?


I know this is asking a lot, but I have researched quite a few ways on looking up a column or looking up a row using python within an excel document.

However, these solutions require you to import different packages, when I do not have access to them. My primary motive for this is to utilize a pre-made excel doc with shaders listed within the document and extract them based upon a brief search.

So, has anyone successfully used python to read an excel doc and, if so, have you been able to match named rows with named columns?


EDIT: Answer picked because it's the closest solution, though it doesn't necessarily answer my question. It's will, however, still provide a very reasonable solution given the context that the user, and his/her excel document, will utilize the same columns and rows to get a usable database.

TL;DR:

No easy way to compare columns to rows to get a data set without using an external package. It's easier to just extract data as an array and specify which "column" is which.


Solution

  • You can also read CSV documents using the built-in csv module to read CSV files, so don't need to install any external modules.

    import csv
    with open('names.csv') as csvfile:
         reader = csv.DictReader(csvfile)
         for row in reader:
             print(row['first_name'], row['last_name'])
    

    If you just grab the whole contents of the reader and stuff them into a list, the lists will contain all the rows and each row will be a dictionary using the headers in the first row. Getting to a particular cell would be something like:

    records = []
    with open('names.csv') as csvfile:
         reader = csv.reader(csvfile)
         for row in reader:
             records.append(row)
    
    # cel "A4" would be
    records[0][3]