-4

I have a dictionary in python and I have to remove specific substring of a value:

dict1 = {'property': 'contents and properties', 'content': 'content metaData creationdate21023415:22:24creationdate metaDataend headingContent headingContent metaData creationDate22102312:02:05creationDate metaDataend DirContent DirContent'}

I have to remove the substring associated with key 'content'. In that, I want to remove from metaData to metaDataend, and every time there is a re-occurrence of metaData in the same string, I want to remove that as well.

Expected End result:

{'property': 'contents and properties', 'content': 'content headingContent headingContent DirContent DirContent'}
Adil
  • 1
  • 1
  • 2

1 Answers1

2

I agree with jonrsharpe that this is not a place for asking people to do coding on your behalf. But in case you have no idea on which way to head with your problem, here are some hints you could look at:

You can use this to iterate your dictionary (splitting it into key and content):

for key, item in dict.iteritems():
    print "key:", key, " item:", item

That you could have found from this old question: Iterating over dictionaries using 'for' loops

Next you will have to find where the words metaData and metaDataend exist in the "item". This you can do with the help of another older question: find position of a substring in a string

Then all you need to do is some slicing, which is described in this question: Explain Python's slice notation

Community
  • 1
  • 1
Crebit
  • 367
  • 1
  • 4
  • 14