0

I have a problem when trying to convert a string containing special characters to a list using a split and save result to josn. For example:

t="machine,machine\c&"

p=t.split(",")

host = {}
host['name'] = p[0]
host['filespace'] = p[1]

with open('json_host.json', 'w') as file:
    json.dump(host, file, indent=4)

return:

{
    "name": "machine",
    "filespace": "machine\\c&"
}

I need the return conver only one \

{
    "name": "machine",
    "filespace": "machine\c&"
}

I have tried to use a replace but it does not work

Toni
  • 1
  • 2
  • 2
    Does this answer your question? [Why do backslashes appear twice?](https://stackoverflow.com/questions/24085680/why-do-backslashes-appear-twice) – 001 Sep 09 '22 at 12:01

1 Answers1

0

The backslash is only once there

When you print as

print(p)

you get list to string representation where the list looks like you have typed it in your code. The backslash should be escaped. Python knows that there is no combination like \c so allows not escaped variant.

If you print real values of the list, you get as it is:

print(' | '.join(p))

machine |  machine\c&

or char by char:

print(p[1][7], p[1][8], p[1][9])

e \ c
crackcraft
  • 76
  • 1
  • 5