-
Notifications
You must be signed in to change notification settings - Fork 93
/
text.py
37 lines (29 loc) · 959 Bytes
/
text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# coding: utf-8
"""Utilities for working with strings and text."""
from __future__ import print_function, division, unicode_literals, absolute_import
def tonumber(s):
"""
Convert string to number, raise ValueError if s cannot be converted.
"""
# Duck test.
try:
stnum = s.upper().replace("D", "E") # D-01 is not recognized by python: Replace it with E.
# stnum = strip_punct(stnum) # Remove punctuation chars.
return float(stnum) # Try to convert.
except ValueError:
raise
except:
raise RuntimeError("Don't know how to handle type %s: %s" % (type(s), str(s)))
def nums_and_text(line):
"""
Split line into (numbers, text).
"""
tokens = line.split()
text = ""
numbers = []
for tok in tokens:
try:
numbers.append(tonumber(tok))
except ValueError:
text += " " + tok
return numbers, text