String formatting issue (parantheses vs underline)

前端 未结 2 1397
南笙
南笙 2021-01-21 20:57

I got a text file containing all my data

data = \'B:/tempfiles/bla.dat\'

from the text file I\'m listing the column header and their types with

2条回答
  •  灰色年华
    2021-01-21 21:05

    The behaviour is documented, the NameValidator class in lib/_iotools.py which parses the names passed in to genfromtxt:

    class NameValidator(object):
        """
        Object to validate a list of strings to use as field names.
        The strings are stripped of any non alphanumeric character, and spaces
        are replaced by '_'. During instantiation, the user can define a list
        of names to exclude, as well as a list of invalid characters. Names in
        the exclusion list are appended a '_' character.
        Once an instance has been created, it can be called with a list of
        names, and a list of valid names will be created.  The `__call__`
        method accepts an optional keyword "default" that sets the default name
        in case of ambiguity. By default this is 'f', so that names will
        default to `f0`, `f1`, etc.
    

    The relevant line in your case being The strings are stripped of any non alphanumeric character

    You can see the behaviour by calling the NameValidator.validate on a list with other non alphanumeric characters in the names:

    In [17]: from numpy.lib._iotools import NameValidator
    
    In [18]: l = ["foo(1)","bar!!!","foo bar??"]
    
    In [19]: NameValidator().validate(l)
    Out[19]: ('foo1', 'bar', 'foo_bar')
    

    And the same using genfromtxt:

    In [24]: datafile = np.genfromtxt("foo.txt", dtype=[('foo!! bar??', '

提交回复
热议问题