I want to batch convert the result of "string" .split () in Python

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

I want to batch convert the result of "string" .split () in Python
I want to display the progress in Python!
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to color a part of an Excel string in Python
I want to embed a variable in a Python string
I want to grep the execution result of strace
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I tried to summarize the string operations of Python
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to store the result of% time, %% time, etc. in an object (variable)
I want to know the features of Python and pip
The result of installing python in Anaconda
Convert the result of python optparse to dict and utilize it
I want to know the population of each country in the world.
I want to do Dunnett's test in Python
I want to create a window in Python
I want to extract an arbitrary URL from the character string of the html source with python
View the result of geometry processing in Python
(Python Selenium) I want to check the settings of the download destination of WebDriver
I want to convert a table converted to PDF in Python back to CSV
I want to merge nested dicts in Python
I want to sort a list in the order of other lists
I want to leave an arbitrary command in the command history of Shell
[Introduction to Python] Thorough explanation of the character string type used in Python!
I made a program to check the size of a file in Python
I want to customize the appearance of zabbix
Python: I want to measure the processing time of a function neatly
How to pass the execution result of a shell command in a list in Python
[Python] I want to know the variables in the function when an error occurs!
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
What you want to memorize with the basic "string manipulation" grammar of python
I want to crop the image along the contour instead of the rectangle [python OpenCV]
I want to get the file name, line number, and function name in Python 3.4
I want to write in Python! (1) Code format check
I tried to graph the packages installed in Python
How to convert / restore a string with [] in python
I want to easily implement a timeout in python
How to get the number of digits in Python
Even in JavaScript, I want to see Python `range ()`!
Convert the image in .zip to PDF with Python
I want to inherit to the back with python dataclass
I want to fully understand the basics of Bokeh
I want to work with a robot in python.
I want to split a character string with hiragana
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I want to increase the security of ssh connections
[python] Convert date to string
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
I just want to find the 95% confidence interval for the difference in population ratios in Python
I want to replace the variables in the python template file and mass-produce it in another file.
I used Python to find out about the role choices of the 51 "Yachts" in the world.
I tried to find the entropy of the image with python
I want to initialize if the value is empty (python)
I want to specify another version of Python with pyvenv
Find out the apparent width of a string in python
I tried the accuracy of three Stirling's approximations in python
Measure the execution result of the program in C ++, Java, Python.