Iterating over a dictionary in python and stripping white space

后端 未结 7 575
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 01:10

I am working with the web scraping framework Scrapy and I am a bit of a noob when it comes to python. So I am wondering how do I iterate over all of the scraped items which

7条回答
  •  独厮守ぢ
    2021-01-13 01:34

    Assuming you would like to strip the values of yourDict creating a new dict called newDict:

    newDict = dict(zip(yourDict.keys(), [v.strip() if isinstance(v,str) else v for v in yourDict.values()]))
    

    This code can handle multi-type values, so will avoid stripping int, float, etc.

提交回复
热议问题