Python API

Greetings

I am trying to execute the sample API code on GITHUB, in python for me to read and write sheets. 

I don't understand the use of the following line of codes.

Do I need to replace _file_ ? and what do I replace it with ?

_dir = os.path.dirname(os.path.abspath(__file__))

# The API identifies columns by Id, but it's more convenient to refer to column names. Store a map here

column_map = {}

When I try to run the code(in bold) I keep getting errors: 

SyntaxError: unexpected EOF while parsing

# Helper function to find cell in a row

def get_cell_by_column_name(row, column_name):

column_id = column_map[column_name]

return row.get_column(column_id)

column_id = column_map[column_name]

Traceback (most recent call last):

  File "<ipython-input-43-bdf21b82574e>", line 1, in <module>

    column_id = column_map[column_name]

NameError: name 'column_name' is not defined



return row.get_column(column_id)

  File "<ipython-input-44-d03dd4f29042>", line 1

    return row.get_column(column_id)

                                    ^

SyntaxError: 'return' outside function

 

Can someone please explain what I am doing wrong?

 

Thank you

Comments

  • dAVE Inden
    dAVE Inden Employee

    There's no need to change the code on line 9 of the python-read-write-sheet.py file: _dir = os.path.dirname(os.path.abspath(__file__))

    That code gets the full file path for the python-read-write-sheet.py file based on the system it is running on. The _dir variable is used later on line 60 to import the Sample Sheet.xlsx file from within the directory of the sample. Getting the file path of the of the file allows to make sure the import request works on different operating systems and in different locations on those systems. There is more info about this that I found in a StackOverflow post here.

    For the other code, are you running that by itself? That is a helper function and will only work in the context of the who sample since it requires the items to which it refers to exist already.

    How are you running the sample?