Warm tip: This article is reproduced from stackoverflow.com, please click
google-analytics google-analytics-api python

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

发布于 2020-04-15 10:40:58

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'}
Questioner
Ahmad Hasnain
Viewed
53
ZapRowsdower 2020-02-04 06:52

Try

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

That ought to strip out the ticks with whitespace.