Masthead

Splitting up Delimited Strings

1. Introduction

We've seen how to create or "format" strings before and "concatinate" them into larger strings. "Parsing" is when we take strings apart. This is crucial for accessing data that is available in files and web services. Strings are parsed by calling "string functions". We've already used one string function, "format". There are a large number of string functions built into Python to help with formatting, parsing, and searching strings.

Below, we'll start with splitting up strings that contain a common delimiter such as a comma, tab, or "pipe" (|).

2. Split

When we read data from a text file or a web service, the information we want is usually buried within other text. When you created your text files you took values and put them into a tab-delimited or comma-separate file. If you want to get to the data again, you need to parse the text to find what you are interested in.

There are a wide variety of formats for text and a large number of functions for parsing them. However, one of the most common, and easiest to work with, are the text files we just created. The "split()" function will break up a string into individual elements based on a "delimiter" like a tab or comma character. Let's try this first in Python with a string you define:

TheString="Rock,Sand,Shale" # create a string delimited with commas
TheElements=TheString.split(",") # split the string at each comma
print(TheElements) # print the elements in the string
print(TheElements[1]) # print the second element in the string

Additional Resources

Python Documentation: String functions

 

© Copyright 2018 HSU - All rights reserved.