Using a configuration of YAML which is tab indente

2019-08-16 22:58发布

问题:

I was writing a unit test, and I discovered a tool that writes YAML configuration with tab indention, but when I tried to read it using yaml.load(file_object) I get an error saying:

(<unknown>): found character that cannot start any token while scanning for the next token at line 2 column 1

or with the tool I see in terminal:

while scanning for the next token
found character '\t' that cannot start any token
  in "/user/config/settings", line 2, column 1

回答1:

Although tab characters are valid in YAML, they cannot be used in indentation, in neither the current version (1.2, nor in the older 1.1, or 1.0)

That does not imply a tab cannot occur at the start of line, as the following example shows

import sys
import ruamel.yaml

yaml_str = """\
'xxx
\tyyy'
"""

yaml = ruamel.yaml.YAML()
yaml.explicit_start = True
data = yaml.load(yaml_str)
print(data)

which runs without error and gives:

xxx yyy

if you remove the single quotes from the yaml_str, you will however get the error that you got (on line 2, column 1), because the parser has to consider if yyy starts a new token (while scanning the single quoted scalar it doesn't do that).

Without seeing the actual YAML, it is difficult to say defitively, but probalby your tool is to blame. You might get away with replacing the tabs:

with open('yourfile.yaml') as fp:
    data = yaml.load(fp.read().replace('\t', ' '))


标签: python yaml