[newbie questions] if conditions - if isset - if empty

Mahmoud Abdel-Fattah accounts at abdel-fattah.net
Sat Apr 14 18:41:47 EDT 2012


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 <schesis at 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120415/acb7e628/attachment-0001.html>


More information about the Python-list mailing list