0

Currently I have a dictionary that looks like this:

{'https://en.wikipedia.org/w/index.php?title=Template:Coronavirus&action=edit': {'e'}, 'https://en.wikipedia.org/w/index.php?title=COVID-19_pandemic&action=edit': {'[update]'}, 'https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/conditionsanddiseases/datasets/coronaviruscovid19infectionsurveydata': {'ONS'}

I want to remove the non alphanumeric characters at the beginning and end of each key and value so it looks like this:

{http://google.com: [Google, That one search engine], http://purdue.edu: [Purdue University]}

Also one more question. How would I write my cleaned dictionary to a txt file with the values of the dictionary indented below the key? I know this is a lot but I need help please. Thanks

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • But why your dictionaries have sets in them for no apparent reason ? – 0xB00B Feb 04 '21 at 03:53
  • 1
    Possible duplicate of https://stackoverflow.com/questions/1276764/stripping-everything-but-alphanumeric-chars-from-a-string-in-python I'm not sure if you want to deserialize the string to a native python objects first... because in that case, some of the non alphanumeric characters - like @Tanishq Banyal metioned - are sets – Jordan Simba Feb 04 '21 at 03:57
  • This question is indecipherable. I vote to close it. Edit the question to include data that clearly shows how input and output are related. Also post the code that you have already tried. – mhawke Feb 04 '21 at 05:37

1 Answers1

0

Not sure if you want to remove alphanumeric characters or remove payloads from url.

But For Writing to text file you can first make a string out of dictionary and then replace , with ,\n. And then write it to file.

0xB00B
  • 1,598
  • 8
  • 26