Skip to content

Write table with keys containing a dot #299

Closed

Description

@corrantho

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.

Activity

  1. frostming commented on Jul 14, 2023

    @frostming
    Contributor

    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")
  2. corrantho commented on Jul 15, 2023

    @corrantho
    Author

    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
    
  3. corrantho commented on Jul 15, 2023

    @corrantho
    Author

    I think it's fixed by #284
    It appears I'm using an outdated version of tomlkit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions