Skip to content
Advertisement

What is the difference between a string and a byte string?

I am working with a library which returns a “byte string” (bytes) and I need to convert this to a string. Is there actually a difference between those two things? How are they related, and how can I do the conversion? Answer Assuming Python 3 (in Python 2, this difference is a little less well-defined) – a string is a

Python read next()

next() in python does not work. What is an alternative to reading next line in Python? Here is a sample: Running this on a file does not show ‘ne ‘. Answer next() does not work in your case because you first call readlines() which basically sets the file iterator to point to the end of file. Since you are reading

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 to sort collections based on current user locale on a Django site

I need to sort a collection of objects by a utf-8 string property (built via ActiveRecord). Currently the code is sorting by ASCII order via the order_by method, however this needs to be changed to locale.strcoll. Unfortunately using the built in locale functionality requires changing the culture for the entire application, not just the current request. I’ve looked at the

What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)? [duplicate]

This question already has answers here: return, return None, and no return at all? (5 answers) Closed 4 months ago. Let’s assume an iteration in which we call a function without a return value. The way I think my program should behave is explained in this pseudocode: If I implement this in python, it bothers me, that the function returns

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. This brings me to: Is there

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, is equivalent to

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 (like ElementTree and cElementTree). The

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 retrieve or set

Advertisement