I'm running python 3.5. After capturing the output from the terminal it appears slightly different in my Python IDE. It's got additional characters I don't need i.e. '\n' or "b''". I tried to use split() and replace() but nothing is working. What am I doing wrong?
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)
Output:
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'"]
Desired output:
['fsdb', 'fsdb1', 'fsdb_fetish_images$v1', 'fsdb_fetish_images', 'fsdb_fetish_images$v2']