Search code examples
pythongoogle-analyticsgoogle-analytics-api

How to remove extra quotes in python while calling google analytics API


Step 1 : Reading the file

#read the csv file
with open('myfile.csv', 'rt') as csvfile:
    reader = csv.reader(csvfile, dialect='excel')
    reader_list = list(reader)
    for row in reader_list:
        print(row[0])
        print(type(row[0])) #printing the type just for illustration 


output

'171667494.1541516384'
<class 'str'>
'548162985.1541558366'
<class 'str'>
'149398364.1541533183'
<class 'str'>
'888359874.1541543604'
<class 'str'>
'1628297960.1541566771'

Step 2: I want to pass exactly the same string(with single quotes) to a google API as shown below. How do I get rid of that ?

for row in reader_list:
 user_deletion_request_resource.upsert(
            body = {
  "id": {  # User ID.
      "userId": row[0],  # I am passing the strings here 
      "type": "CLIENT_ID"
      }}
        ).execute()

For some reason , API is taking an extra quote as shown below in userId

{'id': {'type': 'CLIENT_ID', 'userId': "'785972698.1540375322'"}, 'deletionRequestTime': '2020-02-03T08:25:16.796Z'}

Solution

  • Try

    "userId": row[0].replace("'", "")
    

    That ought to strip out the ticks with whitespace.