import yaml
import json
import csv
import configparser class IoUtils(object): """
dependency: pip install pyyaml yaml_r: read yaml ,return dict yaml_w: write yaml,data type is dict json_r: read json file ,return dict json_w: write json file ,data type is dict csv_r: read csv file ,return list format is:
[
[header],
[column1,column2,....],
.....
] csv_w: write data into csv file
data format is :
[
[],[],
......
]
or like this format:
[
(),()
......
] read_config: read ini file :
return items of section or object of config or only option value """ def yaml_r(self, filepath) -> dict:
with open(filepath, 'r') as f:
data = yaml.load(f, Loader=yaml.Loader)
return data def yaml_w(self, filepath, data: dict):
with open(filepath, 'w', encoding="utf-8") as f:
yaml.dump(data, f) def json_r(self, filepath) -> dict:
with open(filepath, 'r+')as f:
return json.load(f) def json_w(self, filepath, data: dict):
with open(filepath, "w+", encoding="utf-8")as f:
json.dump(data, f, indent=2, ensure_ascii=False) def csv_r(self, csv_path) -> list: with open(file=csv_path, mode="r")as f:
data = csv.reader(f, dialect='excel', delimiter=',', quotechar='|')
data_set = [i for i in data]
return data_set def csv_w(self, csv_path, data):
with open(file=csv_path, mode='w', newline='')as f:
wt = csv.writer(f)
wt.writerows(data) def readConfig(self, filepath, section=None, option=None,
section_only=False, option_only=False,):
config = configparser.ConfigParser()
config.read(filepath)
if section and section_only:
return config.items(section)
if option and option_only:
return config.get(section=section,option=option)
if not section_only and not option_only:
return config def opens(self, filepath, mode, data=None):
"""r or w file """
if mode == "r" or mode == "r+":
with open(filepath, mode)as file:
lines = file.readlines()
return lines
if mode == "w" or mode == "r+" and not data:
with open(filepath, mode)as file:
file.write(data)

  io

最新文章

  1. .NET 获取客户端的操作系统版本、浏览器版本和IP地址
  2. iOS开发之CocoaLumberjack
  3. html5基础知识点
  4. java中的条件语句(if、if...else、多重if、嵌套if)
  5. Consul Template的简单使用
  6. ASP.NET MVC电子书下载
  7. asp.net使用My97 Date Picker时设置默认起始时间为n年之前的今天
  8. 曲线救国,解决Mac系统下,Android sdk下载失败的问题
  9. 巧用test判断来写shell脚本
  10. Search Bars(一个)
  11. Python日期操作
  12. .bat文件设置IP、DNS
  13. python3 今日大纲 day05
  14. LayaBox IDE 安装后执行项目报错解决方案的一些记录
  15. B - 考试排名
  16. 05. pt-diskstats
  17. Oracle截取字符串函数和查找字符串函数,连接运算符||
  18. WPF DataGrid 分组
  19. python基础===map和zip的用法
  20. Mysql 求时间 between 昨天 and 上个月的今天 等时间函数

热门文章

  1. 在电脑上用chrome浏览器调试android手机里的网页代码时,无法看到本地加载的js文件
  2. 洛谷P1051 谁拿了最多奖学金
  3. 《Mongo权威指南》学习手记
  4. 解决wxpy获取不到指定群聊对象的情况
  5. pytest学习4-fixtures
  6. linq和隐式类型var
  7. Paper: A Novel Time Series Forecasting Method Based on Fuzzy Visibility Graph
  8. Spring家族几大插件
  9. 转载一篇棒棒的AWK教程
  10. ZooKeeper Distributed lock