Created
February 13, 2020 07:38
-
-
Save shyjuzz/b01041bc317cd30f827edc742c962826 to your computer and use it in GitHub Desktop.
Tokenize a string with start and end index in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def tokenize(txt): | |
output = [] | |
tokens = re.split('; |, |\*|\n',txt) | |
offset = 0 | |
for token in tokens: | |
offset = txt.find(token, offset) | |
output.append((token, offset, offset+len(token))) | |
offset += len(token) | |
return output | |
s = 'name, account balance, total equity, assets, liabilities, last update date' | |
for token in tokenize(s): | |
print (token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment