Tag Archives: Python

Python – How to convert a list to a string

There must be already a gazillion articles about how to do this, but one more won’t hurt.

Suppose you have the a list as the following:

In [2]: alist
Out[2]: ['apple', 'banana', 'pear']

To create a string from “alist” with a comma separator, we can do this:

In [3]: ','.join(alist)
Out[3]: 'apple,banana,pear'

Basically, you can place a desired separator in the ”, or omit it totally.

Credit: http://www.decalage.info/en/python/print_list

Install Python 3 on CentOS 6

The latest release of Python 3 is 3.3.2, and there’s no package available on CentOS repo via yum. The following steps explains how to manually get Python 3 onto a CentOS system:

Install development tools

# yum groupinstall "Development Tools"
# yum install zlib-devel bzip2-devel openssl-devel \
ncurses-devel sqlite-devel readline-devel \
tk-devel

Download and install Python

$ wget http://python.org/ftp/python/3.3.2/Python-3.3.2.tar.bz2
$ tar xjf Python-3.3.2.tar.bz2
$ cd Python-3.3.2
$ ./configure --prefix=/opt/python3
$ make
$ make install

Test

/opt/python3/bin/python3 -V
Python 3.3.2

References

Python MySQLdb package for Debian

I try to use MySQLdb module with my Python code for the first time. This is what I see:


import MySQLdb
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named MySQLdb
your code here

For Debian, the package for MySQLdb module is python-mysqldb.

# apt-get install python-mysqldb

Now, let’s verify if MySQLdb module has been correctly installed:

>>> import MySQLdb
>>>

No more import error message, which means you can start using MySQldb module now.