欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python操作toml文件的示例代码

程序员文章站 2022-08-10 19:18:27
# -*- coding: utf-8 -*-# @time : 2019-11-18 09:31# @author : cxa# @file : toml_demo.py# @software: p...
# -*- coding: utf-8 -*-
# @time : 2019-11-18 09:31
# @author : cxa
# @file : toml_demo.py
# @software: pycharm
import toml
import os

base_dir = os.path.dirname(os.path.abspath(__file__))


class fileoperation:
  def __init__(self):
    self.dic = dict()
    self.toml_file_path = os.path.join(base_dir, "config.toml")

  def __add__(self, other):
    self.dic.update(self.other)
    return self.dic

  def write(self):
    mysql_dic = {"user": "root", "password": "aa1234"}
    mysql2_dic = {"user1": "root", "password2": "aa1234"}
    mysql_dic.update(mysql2_dic)
    with open(self.toml_file_path, "w", encoding="utf-8") as fs:
      toml.dump(mysql_dic, fs)

  def read(self):
    with open(self.toml_file_path, "r", encoding="utf-8") as fs:
      t_data = toml.load(fs)
    return t_data


if __name__ == '__main__':
  f = fileoperation()
  data = f.read()
  print(data)

以上就是python操作toml文件的示例代码的详细内容,更多关于python操作toml文件的资料请关注其它相关文章!

相关标签: python toml