gspread - Getting values as string from numeric like column

Viewed 1544

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?

2 Answers

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.

You can prevent gspread from casting values to int passing the numericise_ignore parameter to the get_all_records() method.

You can disable it for a specific list of indices in the row:

# Disable casting for columns 1, 2 and 4 (1 indexed):
sheet.get_all_records(numericise_ignore=[1, 2, 4])

Or, disable it for the whole row values with numericise_ignore set to 'all' :

sheet.get_all_records(numericise_ignore=['all'])
Related