Write table with keys containing a dot
Closed this issue · 3 comments
corrantho commented
Hello,
I have the below code
doc = tomlkit.document()
a = tomlkit.aot()
t = tomlkit.table()
t.add("name", "test")
t.add("a.prop1", "value1")
t.add("a.prop2", "value2")
a.append(t)
doc.add("test", a)
print(tomlkit.dumps(doc))
I'm trying to add to a table some elements containing some dots.
I'm expecting the below result:
[[test]]
name = "test"
a.prop1 = "value1"
a.prop2 = "value2"
but I'm getting
[[test]]
name = "test"
"a.prop1" = "value1"
"a.prop2" = "value2"
There are extra double quotes I don't want.
Any idea how am I using wrongly the tomlkit library?
Thanks in advance.
frostming commented
use tomlkit.key([key1, key2])
to get dotted key, or it will be regarded as a single key. The example would be:
t.add(tomlkit.key(["a", "prop1"]), "value2")
corrantho commented
Thank you for your response.
It works fine when I add one property as below
import tomlkit
doc = tomlkit.document()
a = tomlkit.aot()
t = tomlkit.table()
t.add("name", "test")
t.add(tomlkit.key(["a", "prop1"]), "value1")
a.append(t)
doc.add("test", a)
print(tomlkit.dumps(doc))
But as soon as I add a second property, it fails.
import tomlkit
doc = tomlkit.document()
a = tomlkit.aot()
t = tomlkit.table()
t.add("name", "test")
t.add(tomlkit.key(["a", "prop1"]), "value1")
t.add(tomlkit.key(["a", "prop2"]), "value2")
a.append(t)
doc.add("test", a)
print(tomlkit.dumps(doc))
With the error
Traceback (most recent call last):
File "test-tomlkit.py", line 9, in <module>
t.add(tomlkit.key(["a", "prop2"]), "value2")
File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\items.py", line 1385, in add
return self.append(key, value)
File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\items.py", line 1491, in append
self._value.append(key, _item)
File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\container.py", line 188, in append
self._handle_dotted_key(key, item)
File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\container.py", line 150, in _handle_dotted_key
self.append(name, value)
File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\container.py", line 243, in append
self._table_keys[-1] != current_body_element[0]
IndexError: list index out of range