Python 从配置文件中提取值并作为参数传递
这是我的配置文件:Python 从配置文件中提取值并作为参数传递,python,python-2.7,Python,Python 2.7,这是我的配置文件: [account1] username = user1 password = pwd1 [account2] username = user2 password = pwd2 [account3] username = user3 password = pwd3 以下是我需要做的: 读取confile文件,提取用户名和密码,并将它们作为参数传递给另一个脚本。 我想提取这些值,并将它们动态地传递给第二个脚本。 我不想在两者之间再建一本词典。 这是我的密码: #!/
[account1]
username = user1
password = pwd1
[account2]
username = user2
password = pwd2
[account3]
username = user3
password = pwd3
以下是我需要做的:
读取confile文件,提取用户名和密码,并将它们作为参数传递给另一个脚本。
我想提取这些值,并将它们动态地传递给第二个脚本。
我不想在两者之间再建一本词典。
这是我的密码:
#!/usr/bin/python
import ConfigParser
conf = ConfigParser.RawConfigParser()
conf.read('credentials.cfg')
for each_account in conf.sections():
USER = conf.get(each_account, "username")
PASSWORD = conf.get(each_account, "password")
process(USER, PASSWORD)
输出:它使用user3和pwd3只调用一次进程。为什么没有传递其他两个凭据?是否要处理每个用户和密码
#!/usr/bin/python
import ConfigParser
conf = ConfigParser.RawConfigParser()
conf.read('credentials.cfg')
for each_account in conf.sections():
USER = conf.get(each_account, "username")
PASSWORD = conf.get(each_account, "password")
process(USER, PASSWORD)
缩进在Python中很重要。如果对
process(USER,PASSWORD)
的调用缩进不正确,则只会在for
循环完成后调用一次。process(USER,PASSWORD)
在循环之外。也许您应该缩进它?您在循环的每次迭代中都会覆盖您的值,但在循环结束后调用进程
,因此它只在最新的值上运行一次。也许您应该将过程
放入循环中