[英]How to remove non-alphanumeric characters from keys and values in a dictionary and then write it to a text file?
目前我有一本看起來像這樣的字典:
{'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'}
我想刪除每個鍵和值的開頭和結尾的非字母數字字符,所以它看起來像這樣:
{http://google.com:[谷歌,那個搜索引擎], http://purdue.edu :[普渡大學]}
還有一個問題。 我如何將清理后的字典寫入 txt 文件,字典的值縮進在鍵下方? 我知道這很多,但我需要幫助。 謝謝
不確定是否要刪除字母數字字符或從 url 中刪除有效負載。
但是對於寫入文本文件,您可以先從字典中創建一個字符串,然后將,
替換為,\n
。 然后將其寫入文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.