我正在尝试使用 gspread 库使用 python 阅读谷歌表。
初始身份验证设置已完成,我可以阅读相应的表格。
但是,当我这样做时
sheet.get_all_records()
包含类似数值的列(例如 0001,0002,1000)被转换为数值字段。那就是前导零被截断。如何防止这种情况发生?
您可以阻止 gspread 将值转换为 int 将numericise_ignore参数传递给 get_all_records() 方法。
您可以为行中的特定索引列表禁用它:
# Disable casting for columns 1, 2 and 4 (1 indexed):
sheet.get_all_records(numericise_ignore=[1, 2, 4])
或者,将 numericise_ignore 设置为'all'的整行值禁用它:
sheet.get_all_records(numericise_ignore=['all'])
这个答案怎么样?在此答案中,作为几种解决方法之一,get_all_values()
使用get_all_records()
. 检索到值后,将数组转换为列表。请认为这只是几个答案之一。
values = worksheet.get_all_values()
head = values.pop(0)
result = [{head[i]: col for i, col in enumerate(row)} for row in values]
如果这不是你想要的方向,我很抱歉。