How to Split a Line of Text in Python?


Have you ever needed to process a file line by line, but found that some lines were too long and needed to be split up? Maybe you’re processing CSV files and need to split up lines that have too many fields.

Or maybe you’re reading in log files and need to deal with lines that have been wrapped. In any case, Python has a built-in function that can help you with this called “splitlines.”

The splitlines() function

The splitlines() function takes a string and returns a list of strings, broken up at the line boundaries. For example, if we have a string like this:

line = "This is a test.\r\nAnd another test."
Code language: Python (python)

We can call splitlines() on it like this:

>>> line.splitlines() ['This is a test.', 'And another test.']
Code language: Python (python)

As you can see, the function returns a list of two strings, each corresponding to one of the original input string’s lines.

Note that if the input string ends with an empty line, it will be omitted from the output list.

How to split a file into a list in Python

Splitting the file into a list, each item in the list is a line of the file.

My fruits file: myfruits.txt

This is my apple This is my lemon This is my Banana

And here is the python script to read that lines to list.

myfile = open("myfruits.txt", "r") content = myfile.read() content_list = content.splitlines() f.close() print(content_list)
Code language: Python (python)

Output:

['This is my apple', 'This is my lemon', 'This is my Banana']
Code language: Python (python)

Use str.rstrip() TO REMOVE LATERAL NEWLINES

To open a file for reading, use open(file, mode) with mode set to “r”.

Use a for-loop and a list comprehension to go through the file line by line, and then call str.rstrip(chars), where str is each line and chars are ‘n’.

Use file.close() to close file.

My fruits file: myfruits.txt

This is my Strawberry This is my Orange
myfile = open("myfruits.txt", "r") content_list = [line.rstrip('\n') for line in myfile] myfile.close() print(content_list)
Code language: Python (python)

Output

['This is my Strawberry', 'This is my Orange']
Code language: JSON / JSON with Comments (json)

Split() function

You can call split() function with an optional argument, which is the character(s) to use as the line delimiter(s). So if we have a string like this:

line = "First field, second field, 2, 3, 4"
Code language: JavaScript (javascript)

and we want to split it on commas instead of newlines, we could do it like this:

>>> line.split(",") # Use comma as delimiter character(s) ['First field', 'second field', '1', '2', '3', '4']
Code language: CSS (css)

As you can see from the output, specifying a delimiter character (or characters) causes the function to treat any sequence of those characters as one line separator, regardless of whether it occurs at the beginning or end of a line. So commas at the beginning or end of a line are treated just like commas inside the line.

Some example about split() function

Example 1:

line = "Marry@pc-savvy.com" data = line.split("@") print(data)
Code language: Python (python)

Output:

['Marry', 'pc-savvy.com']
Code language: JSON / JSON with Comments (json)

As you can see that it exactly splits the line at the @ symbol.

Example 2:

The line split() function takes a maxsplit parameter to specify how many splits to make. Let’s pass a 1 and see how it splits.

line = "Apple||Strawberry||Banana||Lemon" data = line.split("||", 1) print(data)
Code language: Python (python)

Output:

['Apple', 'Strawberry||Banana||Lemon']
Code language: JSON / JSON with Comments (json)

You can see that we gave 1 as a second parameter and it only splits at one || symbol. The rest are in the second list item’s same line.

Example 3:

Python code for extracting individual lines from a text file

Use With statement and open() method to read a file in Python.

myfruits.txt file contains the following:

"Apple||Strawberry||Banana||Lemon"
Code language: JSON / JSON with Comments (json)

Now, we’ll read this file with open() and use split() to break the line into a list and print it.

with open("myfruits.txt", "r") as ext_file: for line in ext_file: data = line.split('||') print(data)
Code language: Python (python)

Output

['Apple', 'Strawberry', 'Banana', 'Lemon']
Code language: JSON / JSON with Comments (json)

Conclusion:

Splitlines() and Split() are handy tools for working with text data in Python. Next time you find yourself needing to process a file line by line, give it a try!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts