Python syntax may seem strange at the beginning for Java developers. Wouldn’t it be nice if we first see how Python does the common jobs that we have done using other programming languages, like Java? The common code fragments are call “code idioms”. Reading the code idioms of a programming language is often helpful, and can be served as a shortcut for learning a new programming language.
Python
Python read data from MySQL database
The following code can read data from MySQL database. import MySQLdb # Open database connection db = MySQLdb.connect("localhost","username","password","DBName") # prepare a cursor object using cursor method cursor = db.cursor() sql = "select * from table1" cursor.execute(sql) results = cursor.fetchall() for row in results: print row[0] # disconnect from server db.close()import MySQLdb … Read more
Python read file line by line
How to read a text file line by line using Python? Reading files in python is extremely simple to do. The following is the code: with open("/path/to/file.txt") as f: for line in f: linedata = line.split(’\t’)with open("/path/to/file.txt") as f: for line in f: linedata = line.split(‘\t’) Very often, you may want to split each line … Read more
Python: Convert Image to String, Convert String to Image
To store or transfer an image, we often need to convert an image to a string in such a way that the string represents the image. Like other programming languages (e.g. Java), we can also convert an image to a string representation in Python.
Java vs. Python (2): Data Types
If you know Java and want to quickly get a sense of how to use Python from the very beginning, the following summary can provide you a quick review of data types. You may also find the previous comparison of Java and Python useful. By comparing data types between Java and Python, you can get … Read more
Java vs. Python (1): Simple Code Examples
Some developers have claimed that Python is more productive than Java. It is dangerous to make such a claim, because it may take several days to prove that thoroughly. From a high level view, Java is statically typed, which means all variable names have to be explicitly declared. In contrast, Python is dynamically typed, which … Read more
Error message during installation of PyDev for Eclipse
Eclipse version: Indigo Platform: Ubuntu 11.10 Error message: An error occurred while collecting items to be installed session context was:(profile=epp.package.java, phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=, action=). No repository found containing: osgi.bundle,javax.mail.glassfish,1.4.1.v201005082020 This is possibly caused by in compatible eclipse version. If we use Help -> Eclipse MarketPlace to install PyDev, the problem is gone.
Python “Hello World” web application in Ubuntu
This post shows how to setup a python web development environment under Ubuntu and make a hello world example. So Python has become a hot word in recent years. But before you can do anything about it, you need a “hello world” sample application. In this post, I will build a hello world Python web … Read more