6

I am trying to read a YAML file in Python, but due to '\' in the path it is considering that as a hexadecimal number and hence failing, Here is my code

import yaml

def parse_yaml(file_path):
    with open(file_path) as stream:
        yaml_dict = None
        try:
            yaml_dict = yaml.safe_load(stream)
        except yaml.YAMLError as exc:
            print(exc)
    return yaml_dict


file_path = r"C:\\Users\\user\\PycharmProjects\\testautomation\\conf_windows\\generic_configs.yml"
print(parse_yaml(file_path))

Error message:

Error

while scanning a double-quoted scalar
  in "C:\\Users\\user\\PycharmProjects\\testautomation\\conf_windows\\generic_configs.yml", line 2, column 16
expected escape sequence of 8 hexdecimal numbers, but found 's'
  in "C:\\Users\\user\\PycharmProjects\\testautomation\\conf_windows\\generic_configs.yml", line 2, column 21
None

I tried giving path in forward slash and backward slash. Even tried using os.path but nothing worked. The same code is working fine on Mac but failing on Windows.

yaml file content

batchwrite:
  input_file : "/Users/user/Documents/Codes/testautomation/input/batch_write_input.xlsx"
  output_path : "/Users/user/Documents/Codes/testautomation/output"
  dml_file : "/Users/user/Documents/Codes/testautomation/conf/info.dml"
  file_type_yml : "/Users/user/Documents/Codes/testautomation/conf/fields.yml"
him
  • 111
  • 1
  • 10
  • Why does it say `line 2, column 16`, yet in your code this line is clearly not the second one, or even close to that? – ForceBru Jan 11 '20 at 19:51
  • I am not really sure, but could it be that yaml file again contains the paths? Added yaml file content in original question – him Jan 11 '20 at 19:55
  • Please post the _full_ traceback - the whole thing that starts with `Traceback (most recent call last)` – ForceBru Jan 11 '20 at 20:04
  • This is all the error it gives. (venv_windows) C:\Users\user\PycharmProjects\testautomation\batchwrite>python yaml_parser.py while scanning a double-quoted scalar in "C:/Users/user/PycharmProjects/testautomation/conf_windows/generic_configs.yml", line 2, column 16 expected escape sequence of 8 hexdecimal numbers, but found 's' in "C:/Users/user/PycharmProjects/testautomation/conf_windows/generic_configs.yml", line 2, column 21 None – him Jan 11 '20 at 20:08
  • 3
    Your YAML file does not contain any backslashes, `r"…"` is not YAML syntax and line 2, column 16 is not `s`. Are you sure you're showing the actual content of your YAML file? – flyx Jan 11 '20 at 22:09
  • @flyx Isn't `r"...."` is a perfectly fine plain scalar and thus valid YAML syntax? (I think you are right about the YAML file presented, not being the input to the program throwing the error). – Anthon Jan 12 '20 at 10:37
  • @Anthon Yes, I just wanted to point out that it does not invoke any special YAML syntax rule while it does in Python, so chances are high that the author tries to invoke Python semantics and will get a value different from what they want. – flyx Jan 12 '20 at 11:31
  • Thanks @flyx - I was so caught up with the fact that I am giving the wrong path in the program and changed the wrong YAML file. After adding r".." in correct YAML file, it worked. – him Jan 12 '20 at 12:13

2 Answers2

4

I ran into this issue on Windows as well. The suggestion to put my string in my YAML file as r"<string>" did not work for me.

Instead, using single quotes in my YAML file worked. So the following worked for me as expected with no hexadecimal number concerns:

batchwrite:
  input_file : '/Users/user/Documents/Codes/testautomation/input/batch_write_input.xlsx'
  output_path : '/Users/user/Documents/Codes/testautomation/output'
  dml_file : '/Users/user/Documents/Codes/testautomation/conf/info.dml'
  file_type_yml : '/Users/user/Documents/Codes/testautomation/conf/fields.yml'

Relevant section from the PyYAML docs:

Using single-quoted scalars, you may express any value that does not contain special characters. No escaping occurs for single-quoted scalars except that a pair of adjacent quotes '' is replaced with a lone single quote '.

Double-quoted is the most powerful style and the only style that can express any scalar value. Double-quoted scalars allow escaping. Using escaping sequences \x* and \u***, you may express any ASCII or Unicode character.

(The word "scalars" here does not refer to numbers the way it would in a math class. It means something like a single value as opposed to a list or dictionary. The scalars in the example are the paths, which are all strings.)

Reid
  • 85
  • 8
0

The error was referring to the path that I gave in the YAML file, instead of the path that I gave the code. After adding r"..." to the string content in YAML, It worked like charm. Updated content of YAML file

batchwrite:
  input_file : r"/Users/user/Documents/Codes/testautomation/input/batch_write_input.xlsx"
  output_path : r"/Users/user/Documents/Codes/testautomation/output"
  dml_file : r"/Users/user/Documents/Codes/testautomation/conf/info.dml"
  file_type_yml : r"/Users/user/Documents/Codes/testautomation/conf/fields.yml"
him
  • 111
  • 1
  • 10