温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How to remove extra quotes in python while calling google analytics API
google-analytics google-analytics-api python

其他 - 如何在调用Google Analytics API时在python中删除多余的引号

发布于 2020-04-15 23:45:39

步骤1:读取文件

#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 


输出

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

步骤2:我想将完全相同的字符串(带单引号)传递给google API,如下所示。我该如何摆脱呢?

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()

由于某些原因,API会使用额外的引号,如下所示的userId

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

查看更多

提问者
Ahmad Hasnain
被浏览
71
ZapRowsdower 2020-02-04 06:52

尝试

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

那应该用空格去除刻度线。