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.

100000,100000,120000,150000,170000,170000,80000,50000

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.