The .desktop spec requires keeping comments, so I can use a Python library that supports them listed below.
It's this one: https://codeberg.org/JakobDev/desktop-entry-lib
I had to go through the developer's desktop file editor project to find the library that was in use but I don't remember the code, to be fully transparent on this. Hopefully it's ok because I don't remember any of the rest of the code I read even though the project is under GPLv3 (I'm planning on switching to it where necessary, anyway). Even then, I purposefully skimmed it to not internalize it.
This library is under the BSD 2-clause license.
I may have to figure out how it's keeping comments and extract that so I can have my config files keep their comments if I can't figure out anything else.
Or maybe I could use ConfigUpdater? https://github.com/pyscaffold/configupdater
Only issue is it doesn't support duplicate sections and keys, a major issue considering one of the Gnome .desktop files has a duplicate key.