迭代一个字典在python和剥离的空白 [英] Iterating over a dictionary in python and stripping white space
问题描述
我正在使用Web Scratch框架Scrapy,而对于python来说,我是一个noob。所以我想知道如何迭代所有似乎在字典中的被刮的项目,并从每个项目中删除空白。
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 seem to be in a dictionary and strip the white space from each one.
这是代码我在我的物品管道中玩过:
Here is the code I have been playing with in my item pipeline.:
for info in item:
info[info].lstrip()
但这段代码不起作用,因为我不能单独选择项目。所以我试图这样做:
But this code does not work, because I cannot select items individually. So I tried to do this:
for key, value item.items():
value[1].lstrip()
第二种方法在某种程度上起作用,但问题是我不知道如何然后循环所有的值。
This second method works to a degree, but the problem is that I have no idea how then to loop over all of the values.
我知道这可能是一个简单的修复,但我似乎找不到。任何帮助将不胜感激。 :)
I know this is probably such an easy fix, but I cannot seem to find it. Any help would be greatly appreciated. :)
推荐答案
不是直接回答这个问题,而是建议你看一下 Item Loaders 和输入/输出处理器。很多你的清理可以在这里照顾。
Not a direct answer to the question, but I would suggest you look at Item Loaders and input/output processors. A lot of your cleanup can be take care of here.
剥离每个条目的例子是:
An example which strips each entry would be:
class ItemLoader(ItemLoader):
default_output_processor = MapCompose(unicode.strip)
这篇关于迭代一个字典在python和剥离的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!