Green
Green

Reputation: 2565

Python: how to split a string by a delimiter that is invalid <0x0c>

I have a document which contain the charterer <0x0c>. Using re.split.
The problem that it look like that:

import re
re.split('',text) 

When although it works, you CAN'T see the charterer and except of living a nice comment it is a great candidate to be one of this legacy code that only I would understand.

How can I write it in a different, readable way?

Upvotes: 0

Views: 564

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122392

You can express any character using escape codes. The 0x0C Form Feed ASCII codepoint can be expressed as \f or as \x0c:

re.split('\f', text)

See the Python string and byte literals syntax for more details on what escape sequences Python supports when defining a string literal value.

Note: you don't need to use the regex module to split on straight-up character sequences, you can just as well use str.split() here:

text.split('\f')

Upvotes: 1

Related Questions