Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I return cell values in a spreadsheet based on user input in Python?

Tags:

python

regex

My goal is to ask a user for a date, then scan through the cell values of a spreadsheet column and return any cells that start with that date.

I have a spreadsheet in which the first column is a list of timestamps (e.g., 10/29/2012 "9:10:12"). I'm using Python and gpsread to access the Google spreadsheet.

import gspread
import re

gc = gspread.login('username', 'password') # login
wks = gc.open('googleSpreadsheet').sheet1 # open spreadsheet

I'm trying to ask the user for a date input like this:

dateInput = str(raw_input('Date? (MM/DD/YYYY) '))

I then want to take that input and use it in a regular expression to find all the cells that start with it:

amount_re = re.compile(r'(^dateInput)')
cell_list = wks.findall(amount_re)

but that returns an empty array.

So, how do I take a user input and return any cells that start with that input? Ultimately, I want to be able to find any cells in the spreadsheet that have a given date, then copy and paste the corresponding rows to a different spreadsheet. This first part of parsing through the data and storing the cells is where I'm hung up.

like image 916
kevingduck Avatar asked Aug 12 '26 01:08

kevingduck


1 Answers

If you're actually pulling the user input in the way you've presented, the problem might be a trailing newline. Try:

dateInput = raw_input("Date? (MM/DD/YYYY) ").strip()
amount_re = re.compile(r"(^%s)" % (dateInput,))
cell_list = wks.findall(amount_re)
like image 155
BenTrofatter Avatar answered Aug 14 '26 14:08

BenTrofatter