简体   繁体   English

使用 python requests.get 将变量传递给参数

[英]Passing a variable to params with python requests.get

I am currently sending an api request and am using the following params:我目前正在发送 api 请求并使用以下参数:

params = (
    ('adminDeviceSpaceId', '1'),
    ('fields', 'special.field'),
    ('query', 'user.email_address="myemail@mymail.com"'),
)

requests.get works fine, i need to replace the email with a variable (passed in from another program). requests.get 工作正常,我需要用一个变量(从另一个程序传入)替换 email。 When i replace the email= with当我将 email= 替换为

EMAIL_VARIABLE = 'myemail@mymail.com'
('query', 'user.email_address="(EMAIL_VARIABLE)"'),

it does not work, tried every permutation of removing and changing quotes.它不起作用,尝试了删除和更改引号的所有排列。 Has to be something simple.必须是简单的东西。

It is on RHEL7 so it is python 2.7.5.它在 RHEL7 上,所以它是 python 2.7.5。

In Python 2.7 you can use:在 Python 2.7 中,您可以使用:

'user.email_address="{}"'.format(EMAIL_VARIABLE)

For anyone using Python 3对于使用 Python 3 的任何人

https://realpython.com/python-f-strings/ https://realpython.com/python-f-strings/

f"user.email_address={EMAIL_VARIABLE}"

Usage:用法:

>>> print(f'user.email_address={EMAIL_VARIABLE}')
user.email_address=myemail@mymail.com

Python 2.7 Python 2.7

print 'user.email_address={}'.format(EMAIL_VARIABLE))

or或者

print 'user.email_address={0}'.format(EMAIL_VARIABLE))

https://pyformat.info/#simple https://pyformat.info/#simple

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM