Thanks ChrisA a lot. I just tried what you said, but I got an error, here's what I tried : item['name'] = hxs.select('//div[@id="center-main"]/h1/text()').extract()[0] author_brand = re.match(r'^[.*] - (.*)$', item['name'], re.I|re.S|re.M) item['author_brand'] = author_brand.group(2) if type(author_brand) != None else ''
And I'm getting the following error : AttributeError: 'NoneType' object has no attribute 'group' Thanks again :) On Sun, Apr 15, 2012 at 12:35 AM, Zero Piraeus <sche...@gmail.com> wrote: > : > > > 1. How can I write the following code in easier way in Python ? > > if len(item['description']) > 0: > > item['description'] = item['description'][0] > > else: > > item['description'] = '' > > Assuming item['description'] is a string, all you need is > > >>> item['description'] = item['description'][:1] > > Read up on string/sequence slicing if it isn't clear why that works. > > > 2. How can I check if this variable defined or not, in PHP I can use > > isset();, in other way, how can I make the following syntax in Python ? > > $variable = isset($array['element']) ? true : false; > > For that example: > > >>> variable = 'element' in array > > In both cases, a more general solution would be more complex ... note > that in your second question, you're not actually testing whether a > variable is set, but whether a dict has a value for a particular key. > > -[]z. >
-- http://mail.python.org/mailman/listinfo/python-list