Issue with "ValueError: Single '}' encountered in format string" in an API Call
Issue with "ValueError: Single '}' encountered in format string" in an API Call
我为此使用 Python 3。
基本上,我正在使用 urllib 进行 API 调用并收到错误:
"ValueError: Single '}' encountered in format string"
我已经查看了各种其他解决方案,但它们似乎都不起作用。
基本上我在做的是:
import urllib.request
import urllib.parse
def query_person(first, last):
person_request = urllib.request.urlopen('http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27{last}}%27%29%20AND%20authfirst%28%27{first}}%27%29&'.format(first=first,last=last))
return(person_request)
print(query_person("John", "Doe"))
实际的 API 将无法重现,因为它需要一个 API 密钥(明显省略)以及需要在经过验证的网络上。
我认为问题与 "{last}}%27%29%20AND%20authfirst%28%27{first}}" 有一个额外的括号有关。例如,如果我只想在我的 url 栏中查询它而没有 python 或 .format(),它看起来像:
http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27Doe}%27%29%20AND%20authfirst%28%27John}%27%29&
或更具体地说:Doe}%27%29%20AND%20authfirst%28%27John}%27%29&
如果我在python中使用后一种方法,我没有问题,但它当然不允许我输入名称进行查询。
如果你想让它留在字符串中,你需要加倍在你的单个括号上:
例如:
'{first}}}'.format(first='John') == 'John}'
你的情况:
person_request = urllib.request.urlopen('http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27{last}}}%27%29%20AND%20authfirst%28%27{first}}}%27%29&'.format(first=first,last=last))
我为此使用 Python 3。
基本上,我正在使用 urllib 进行 API 调用并收到错误:
"ValueError: Single '}' encountered in format string"
我已经查看了各种其他解决方案,但它们似乎都不起作用。
基本上我在做的是:
import urllib.request
import urllib.parse
def query_person(first, last):
person_request = urllib.request.urlopen('http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27{last}}%27%29%20AND%20authfirst%28%27{first}}%27%29&'.format(first=first,last=last))
return(person_request)
print(query_person("John", "Doe"))
实际的 API 将无法重现,因为它需要一个 API 密钥(明显省略)以及需要在经过验证的网络上。
我认为问题与 "{last}}%27%29%20AND%20authfirst%28%27{first}}" 有一个额外的括号有关。例如,如果我只想在我的 url 栏中查询它而没有 python 或 .format(),它看起来像:
http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27Doe}%27%29%20AND%20authfirst%28%27John}%27%29&
或更具体地说:Doe}%27%29%20AND%20authfirst%28%27John}%27%29&
如果我在python中使用后一种方法,我没有问题,但它当然不允许我输入名称进行查询。
如果你想让它留在字符串中,你需要加倍在你的单个括号上:
例如:
'{first}}}'.format(first='John') == 'John}'
你的情况:
person_request = urllib.request.urlopen('http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27{last}}}%27%29%20AND%20authfirst%28%27{first}}}%27%29&'.format(first=first,last=last))