Python – ‘ascii’ codec can’t decode byte

encode converts a unicode object to a string object. But here you have invoked it on a string object (because you don’t have the u). So python has to convert the string to a unicode object first. So it does the equivalent of But the decode fails because the string isn’t valid ascii. That’s why you get a complaint about not being able to … Read more

How do you use subprocess.check_output() in Python?

The right answer (using Python 2.7 and later, since check_output() was introduced then) is: To demonstrate, here are my two programs: py2.py: py3.py: Running it: Here’s what’s wrong with each of your versions: First, str(‘python py2.py’) is exactly the same thing as ‘python py2.py’—you’re taking a str, and calling str to convert it to an str. This makes the code harder to read, longer, … Read more

How can I concatenate str and int objects?

The problem here is that the + operator has (at least) two different meanings in Python: for numeric types, it means “add the numbers together”: … and for sequence types, it means “concatenate the sequences”: As a rule, Python doesn’t implicitly convert objects from one type to another1 in order to make operations “make sense”, because that would … Read more

How do you use subprocess.check_output() in Python?

The right answer (using Python 2.7 and later, since check_output() was introduced then) is: To demonstrate, here are my two programs: py2.py: py3.py: Running it: Here’s what’s wrong with each of your versions: First, str(‘python py2.py’) is exactly the same thing as ‘python py2.py’—you’re taking a str, and calling str to convert it to an str. This makes the code harder to read, longer, … Read more

No module named MySQLdb

You need to use one of the following commands. Which one depends on what OS and software you have and use. easy_install mysql-python (mix os) pip install mysql-python (mix os/ python 2) pip install mysqlclient (mix os/ python 3) apt-get install python-mysqldb (Linux Ubuntu, …) cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD) yum install MySQL-python (Linux Fedora, CentOS …) For Windows, … Read more