Like this
a, b, c = "hello 3.14 0".split()
b = float(b)
c = int(c)
It's a hassle, so I want a way to do it in one shot ... Maybe everyone is reinventing the wheel, right?
@Odashi_t on twitter
The process of reading lines in Python, separating them with spaces, and reading values in each variable, always a,b,c,d,e,... = line.split() After that, I've converted each one, but is there any easy way to write it?
I don't need anything as sophisticated as a regular expression, so a,b,c,d,e = split_and_convert(line, (int, float, str, str, int)) I'd be happy if I could write something like>.
I also wanted to know your smart way, so I will post the one I am using
def map_apply(proc, args):
# return [f(x) for f, x in zip(proc, args)]
return map(lambda f,x:f(x), proc, args) # map(apply, proc, args) doesn't work like this
class FieldConverter:
def __init__(self, *args):
self._converters = args
def conv_proc(f):
def _wrap(conv_proc_to_num):
def proc(x):
try:
return conv_proc_to_num(x)
except ValueError:
return 0
return proc
if f is None:
return lambda s:s # identity
elif isinstance(f, str):
return lambda s:s.decode(f) # -> unicode
elif f in (int, float):
return _wrap(f)
else:
return f
if len(args) > 0:
self.converters = [conv_proc(f) for f in self._converters]
else:
self.converters = None
def convert(self, fields):
if self.converters:
return map_apply(self.converters, fields)
else:
return fields
def test_map_apply():
assert [0, -1, 3.14, 5.0] == map_apply([int,int,float,float], ['0','-1','3.14','5'])
assert [0,u'Japanese','Japanese'] == map_apply([int,lambda s:s.decode('utf-8'),lambda s:s], ['0','Japanese','Japanese'])
def test_field_converter():
assert [0, -1, 3.14, 5.0] == FieldConverter(int,int,float,float).convert(['0','-1','3.14','5'])
assert [0,u'Japanese','Japanese'] == FieldConverter(int,'utf-8',None).convert(['0','Japanese','Japanese'])
I don't need to explain int and float. If you do'utf-8', it will be decoded in UTF-8 and converted to unicode. None is no conversion You can pass any one-variable function (even lambda).
@ odashi_t's split_and_convert ()
def split_and_convert(line, types, delim=' '):
field_converter = FieldConverter(*types)
return field_converter.convert(line.split(delim))
def test_split_and_convert():
assert [0, -1, 3.14, 5.0] == split_and_convert('0\t-1\t3.14\t5', (int,int,float,float), '\t')
assert [0, u'Japanese', 'Japanese'] == split_and_convert('0,Japanese,Japanese', (int,'utf-8',None), ',')
I wonder if it can be implemented like this. I want to cache field_converter.
If the conversion is just an int or float
def split_and_convert(line, types, delim=' '):
return [f(x) for f,x in zip(types, line.split(delim))]
It's okay though.
If this
a, b, c, d = split_and_convert('0\t-1\t3.14\t5', (int,int,float,float), '\t')
Can be used like. (A = 0, b = -1, t = 3.14, d = 5.0)
For the time being, I will put the code on gist https://gist.github.com/naoyat/3db8cd96c8dcecb5caea
Recommended Posts