【发布时间】:2019-07-16 16:27:01
【问题描述】:
我正在运行 python 3.5。从终端捕获输出后,它在我的 Python IDE 中看起来略有不同。它有我不需要的其他字符,即“\n”或“b”。我尝试使用 split() 和 replace() 但没有任何效果。我究竟做错了什么?
def runIndexTest(zone):
print('Turning OFF flit on ' + zone)
#setIndexStatus(zone, 'stop')
cmd1 = 'psql -h ' + zone + ' -U filmlight -t -c ' + '"' + """SELECT datname FROM pg_database WHERE datistemplate = false AND datname LIKE """ + "'fsdb%'" + ';"'
#print(cmd1)
out = subprocess.run(cmd1, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print(out.stdout)
db_list = str(out.stdout).split(' ')
for db in db_list:
db = db.replace("\n", '')
if db == "b'":
db_list.remove(db)
continue
print('Length of db_list:', len(db_list))
print(db_list)
输出:
b' fsdb\n fsdb1\n fsdb_fetish_images$v1\n fsdb_fetish_images\n fsdb_fetish_images$v2\n\n'
Length of db_list: 5
['fsdb\\n', 'fsdb1\\n', 'fsdb_fetish_images$v1\\n', 'fsdb_fetish_images\\n', "fsdb_fetish_images$v2\\n\\n'"]
期望的输出:
['fsdb', 'fsdb1', 'fsdb_fetish_images$v1', 'fsdb_fetish_images', 'fsdb_fetish_images$v2']
【问题讨论】:
-
我认为你可以使用 str(your_string_here) 函数来转换它。但是如果你使用 decode("utf-8") 它不应该出现
标签: python subprocess output