Encoding Character Or String To Integer In Python

Source URL: N/A

Language: python

Created on: 2020-05-11 13:24:22

note:text contains your string or already read textfile 

import numpy as np
text = 'hello'
vocab=sorted(set(text))
char_to_ind = {char:ind for ind,char in enumerate(vocab)}
encoded_text = np.array([char_to_ind[c]for c in text])

your string has been encoded
Encoding Character Or String To Integer In Python
content_copy