I
I
Ilya Rodionov2017-06-09 21:28:16
Python
Ilya Rodionov, 2017-06-09 21:28:16

How to remove a line from Python text?

Dear users!
There was a question related to python.
You need to remove a row from the original data.
That is: at the input I get data like { 'site': toster.ru, 'ping':200}
At the output I need to write to the variable { 'site': toster.ru,}
And everything would be fine if the ping was always would be dynamic
For with the help of Python and variable.replace('ping', '') I can remove only the word "ping", and its value itself remains.
Tell me how to remove not only the word, but also the value, if the standard 'replace' method does not remove it?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2017-06-09
@ZloyKishechnik

https://docs.python.org/3/library/json.html

from json import loads, dumps

s = '{"foo": "bar"}'
d = loads(s)
print(d)
del d['foo']
print(d)
print( dumps(d) )

E
Eugene, 2017-06-09
@immaculate

import re
re.sub(",\s*'ping'\s*:\s*\d+", "",  "{'site':toster.ru, 'ping':200}")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question