[Python] Expand escape sequences in here documents

I'm addicted to it, so I'll take note of it.

Even if the escape sequence (\) is written as it is in the here document, it is not expanded as an escape sequence.

It can be expanded by writing two in a row like \\ or by using a raw string.

>>> import json
>>>
>>> json_dict1 = json.loads('''
... {
...     "id": 1,
...     "name": "hoge",
...     "discription": "Hoge\"Hoge\"Hoge"
... }
... ''')
Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 5 column 24 (char 59)
>>> print(json_dict1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'json_dict1' is not defined
>>>
>>> json_dict2 = json.loads('''
... {
...     "id": 2,
...     "name": "hoge",
...     "discription": "Hoge\\"Hoge\\"Hoge"
... }
... ''')
>>> print(json_dict2)
{'id': 2, 'name': 'hoge', 'discription': 'Hoge"Hoge"Hoge'}
>>>
>>> json_dict3 = json.loads(r'''
... {
...     "id": 3,
...     "name": "hoge",
...     "discription": "Hoge\"Hoge\"Hoge"
... }
... ''')
>>> print(json_dict3)
{'id': 3, 'name': 'hoge', 'discription': 'Hoge"Hoge"Hoge'}

Recommended Posts

[Python] Expand escape sequences in here documents
MySQL-automatic escape of parameters in python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Python here document
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
[Python] How to expand variables in a character string
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python