A String Parsing Example

To compare Java and Python string parsing, we're going to create a readable file consisting of a comma-delimited list of house prices.


Here's the Python code broken down:

Open the file.

>>> file = open("data.txt")

Read in the file data.

>>> data = file.read()

Import the split() function to parse the data.

>>> from string import split
>>> housePrices = split(data, ",")

For demonstration, show that split() has split the data string into a list of strings.

>>> housePrices
['100000', '100000', '120000', '150000', '170000', '170000', '80000', '50000']

Convert housePrices from strings to floating-point values.

>>> housePrices = map(float, housePrices)

Show that ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.