Skip to content

Tag: python

About mysql cursor and iterator

Imagine I have a mysql cursor and data read. The amount of data might be very big that I want to deal with one line each time. An easy and straight forward way might be like this: But this doesn’t look good, so I wonder whether this way works as imagined: The thing I want to know is: if I

How can I put multiple statements in one line?

I know a little bit of comprehensions in Python, but they seem very hard to ‘read’. The way I see it, a comprehension might accomplish the same as the following code: This code is much easier to read than how comprehensions currently work, but I’ve noticed you can’t have two :s in one line. …

How to test the membership of multiple values in a list

I want to test if two or more values have membership on a list, but I’m getting an unexpected result: So, Can Python test the membership of multiple values at once in a list? What does that result mean? See also: How to find list intersection?. Checking whether any of the specified values is in the list…

selecting attribute values from lxml

I want to use an xpath expression to get the value of an attribute. I expected the following to work but this gives an error : Am I wrong to expect this to work? Answer find and findall only implement a subset of XPath. Their presence is meant to provide compatibility with other ElementTree implementations (l…

Equivalent to InnerHTML when using lxml.html to parse HTML

I’m working on a script using lxml.html to parse web pages. I have done a fair bit of BeautifulSoup in my time but am now experimenting with lxml due to its speed. I would like to know what the most sensible way in the library is to do the equivalent of Javascript’s InnerHtml – that is, to r…

Detect last iteration over dictionary.iteritems() in python

Is there a simple way to detect the last iteration while iterating over a dictionary using iteritems()? Answer This is a special case of this broader question. My suggestion was to create an enumerate-like generator that returns -1 on the last item: Add gen = iter(gen) if you want it to handle sequences as we…