【问题标题】:How to append URLs to an array in python?如何将URL附加到python中的数组?
【发布时间】:2022-07-01 16:15:23
【问题描述】:

如何在 python 中将 URL 附加到数组中,我正在尝试使用以下代码,

zone = ["z1", "z2", "z3"]
result_1 = []

for i in zone:
    result_val = os.system("gcloud command1 " +i+ " --command2")
    result_1.append(result_val)
print(result_1)

但是当运行它时,我的结果如下所示,

https://sample-domain.com/test/abc/1
https://sample-domain.com/test/abc/2
https://sample-domain.com/test/abc/3

但是当我打印“result_1”时,它会显示在结果下方

[0, 0, 0]

但我需要这样的结果,

["https://sample-domain.com/test/abc/1","https://sample-domain.com/test/abc/2","https://sample-domain.com/test/abc/3"]

我该如何解决这个问题?

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 您在那里运行的确切命令是什么?结果是您添加到列表中的退出错误 (0)。
  • @baduker 我得到512,而不是链接

标签: python python-3.x


【解决方案1】:

那是因为你得到的命令返回码是一个整数,你应该改用这个:

import subprocess
res = subprocess.run(["gcloud", "command1", "whatever"], capture_output=True)
print(res.stdout)

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2021-10-12
    • 2017-07-07
    • 1970-01-01
    • 2017-08-18
    • 2017-11-22
    • 2019-08-13
    • 1970-01-01
    相关资源
    最近更新 更多