【问题标题】:Concatenate number sequence with mathematical operator without executing the operator用数学运算符连接数列而不执行运算符
【发布时间】:2019-07-21 22:35:18
【问题描述】:
我必须在 URL http://api.semanticscholar.org/v1/paper/ 的末尾连接论文“10.1145/3175684.3175695”的此 DOI。但是当我尝试这样做时,python 会连接分割的结果。有没有办法告诉python不要将10.1145/3175684.3175695中间的/符号视为除法运算符。
id = 10.1145/3175684.3175695
url = '{}{}'.format("http://api.semanticscholar.org/v1/paper/",id)
# Make a get request with the parameters.
response = requests.get(url)
print(response.content)
【问题讨论】:
标签:
python
python-3.x
string
api
concatenation
【解决方案1】:
问题是由于您没有将 DOI 号括在引号中以使其成为字符串。您会注意到,作为字符串的基本 URL 中包含正斜杠,但没有出现同样的问题。
你有以下:
doi = 10.1145/3175684.3175695
哪个python解释为这样的数学表达式:
doi = 10.1145 / 3175684.3175695
您需要将其用单(或双)引号括起来以使其成为字符串文字:
doi = '10.1145/3175684.3175695'
base_url = 'http://api.semanticscholar.org/v1/paper/'
url = base_url + doi
【解决方案2】:
s = "http://api.semanticscholar.org/v1/paper/"
v = "10.1145/3175684.3175695"
t = s+v
t 将导致
'http://api.semanticscholar.org/v1/paper/10.1145/3175684.3175695'