gsr
gsr

Reputation: 189

python removing special char from dict

Im using python3 and im converting string of output to list. the list ends up having special chars id like to remove and havent found a way to do

this is the script:

bashCommand = "some bash command"
stdoutdata = subprocess.getoutput(bashCommand)
lines = stdoutdata.splitlines()
result = {"data": []}

for line in lines:
    line = line.rstrip()
    entry = line.split(":")
    result["data"].append({"{#NAME}":entry[0],"{#STATUS}": entry[1]})
print(json.dumps(result))

the output from bash command is this:

GENERAL-1 : UP
GENERAL-2 : UP
GENERAL-3 : UP

this is the result:

{"data": [{"{#NAME}": "GENERAL-1", "{#STATUS}": " UP\u001b[0m"}, {"{#NAME}": "GENERAL-2", "{#STATUS}": " UP\u001b[0m"}, {"{#NAME}": "GENERAL-3", "{#STATUS}": " UP\u001b[0m"}]}

Id like to remove the \u001b[0m from the dict. thanks

Upvotes: 0

Views: 41

Answers (1)

ilias-sp
ilias-sp

Reputation: 6685

just parse the entry[1] and replace the \u001b[0m to empty string, modifying this line to:

result["data"].append({"{#NAME}":entry[0],"{#STATUS}": entry[1].replace('\u001b[0m', ''})

Upvotes: 1

Related Questions