I'm using the Google Drive API as a part of an application I'm writing. I currently have a list which contains tuples of the following information
(File name, File ID)
:
example > [(Finance Report - 2017-08-30, 109fADijw_OLojk2nbcxziw2DKn0), (Finance Report - 2017-08-24, 109Xjwj7Fuo_whoqpwl920jAAikLoEnU)]
I'm trying to remove the Finance Report -
from the file name part of each tuple. But something odd is happening, it's also stripping data from the file id part of the tuple. Below is the code fragment for this part of the application.
q_param = "'%s' in parents" % PARENT_ID
files = []
response = drive_service.files().list(q=q_param,
spaces='drive').execute()
for drive_file in response.get('files', []):
files.append((drive_file.get('name'), drive_file.get('id')))
#Removing files that aren't labeled Finance report ...
files = [x for x in files if "Finance report" in x[0]]
#Stripping Finance Report from each file name
files = [tuple(x.strip('Finance report - ') for x in y) for y in files]
I got that specific line from a Stack Overflow post that I found to deal with this kind of need. But below is what the output data looks like after stripping:
[(2017-08-30, 109fADijw_OLojk2nbcxziw2DK), (2017-08-24, 109Xjwj7Fuo_whoqpwl920jAAikLoE)]
Before and after:
109fADijw_OLojk2nbcxziw2DKn0, 109fADijw_OLojk2nbcxziw2DK
I'm not really sure why this is happening as I thought the strip in the list comprehension is being performed on the x[0] of every tuple x
in list files
.
Why is this happening?
You don't need a generator expression for items in the tuple. Apply strip only on the first item:
files = [(x.strip('Finance report - '), y) for x, y in files]
Also, since the part left after stripping is of fixed width, you can take a slice instead of stripping; stripping can be funny at times as it works per character, not on the entire substring:
files = [tuple(x[-10:], y) for x, y in files]