-1

Couldn't find any material on this question.

I have a yaml file containing:

items :
- "jeweller's orb"

I then load it:

with open('./configs/items.yaml') as f:
    data = yaml.safe_load(f)
self.item_set = set([x.upper() for x in data['items']])

Now how do I access the set with a key containing a single quote?? I've tried escaping it, double single quoting it, nothing works...

print('JEWELLER\’S ORB' in self.item_set)
print("JEWELLER\’S ORB" in self.item_set)
print('JEWELLER’S ORB' in self.item_set)
print("JEWELLER’S ORB" in self.item_set)
print(r'JEWELLER’S ORB' in self.item_set)
print(r"JEWELLER’S ORB" in self.item_set)

They all return false. Something with yaml behind the scenes I am not aware of??

I've tried also copying the output of the variables in the self.item_set and directly using it in the print. Still nothing.

EDIT: The answer was

with open('./configs/items.yaml', 'rt', encoding='utf8') as f:

As well as using the opening single quote version in the yaml instead of straight single quote. Wow, what a pain.

Joseph Luce
  • 11
  • 1
  • 3
  • 1
    Try `"JEWELLER'S ORB"` – rdas Jul 14 '20 at 08:09
  • Does this answer your question? [How to escape single quotes in Python on a server to be used in JavaScript on a client](https://stackoverflow.com/questions/3708152/how-to-escape-single-quotes-in-python-on-a-server-to-be-used-in-javascript-on-a) – Rob Evans Jul 14 '20 at 08:09
  • 1
    Also note that the single-quote you've used in the code is not the same as the one in the yaml file. – rdas Jul 14 '20 at 08:10
  • `"JEWELLER'S ORB" in item_set` – bigbounty Jul 14 '20 at 08:10
  • Some (rich text-)editors use unicode quotes instead of the vanilla ascii ones. When you compare those in python, they are not equal (different unicode code-points). Just look at the first try in your question. Those characters are not rendered the same. – rdas Jul 14 '20 at 08:19
  • none of those suggestions are working. – Joseph Luce Jul 14 '20 at 08:20

1 Answers1

-1
with open('./configs/items.yaml', 'rt', encoding='utf8') as f:

As well as using the opening single quote version in the yaml instead of straight single quote. Wow, what a pain.

Joseph Luce
  • 11
  • 1
  • 3