Search code examples
pythongspread

gspread - Getting values as string from numeric like column


I am trying to read a google sheet using python using the gspread library.

The initial authentication settings is done and I am able to read the respective sheet.

However when I do

sheet.get_all_records()

The column containing numeric like values (eg. 0001,0002,1000) are converted as numeric field. That is the leading zeroes are truncated. How to prevent this from happening?


Solution

  • How about this answer? In this answer, as one of several workarounds, get_all_values() is used instead of get_all_records(). After the values are retrieved, the array is converted to the list. Please think of this as just one of several answers.

    Sample script:

    values = worksheet.get_all_values()
    head = values.pop(0)
    result = [{head[i]: col for i, col in enumerate(row)} for row in values]
    

    Reference:

    If this was not the direction you want, I apologize.