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

ioutils的更多相关文章

  1. apache.commons.io.IOUtils: 一个很方便的IO工具库(比如InputStream转String)

    转换InputStream到String, 比如 //引入apache的io包 import org.apache.commons.io.IOUtils; ... ...String str = IO ...

  2. Java程序员的日常—— IOUtils总结

    以前写文件的复制很麻烦,需要各种输入流,然后读取line,输出到输出流...其实apache.commons.io里面提供了输入流输出流的常用工具方法,非常方便.下面就结合源码,看看IOUTils都有 ...

  3. IO - IOUtils

    Commons IO is a library of utilities to assist with developing IO functionality. There are four main ...

  4. Commons IO - IOUtils

    IOUtils is a general IO stream manipulation utilities. This class provides static utility methods fo ...

  5. 文件输入输出流工具: IOUtils使用总结

    序言 以前写文件的复制很麻烦,需要各种输入流,然后读取line,输出到输出流...其实apache.commons.io里面提供了输入流输出流的常用工具方法,非常方便.下面就结合源码,看看IOUTil ...

  6. Delphi 2010 新增功能之: IOUtils 单元(6): TPath(结构体) 的方法与属性

    以后路径相关的处理, 用 IOUtils.TPath 就很方便了. //较常用的方法: TPath.GetTempPath;                  {获取临时文件夹路径} TPath.Ge ...

  7. Apache IOUtils的使用

    IOUtils 与 FileUtilsCommons IO 是 apache 的一个开源的工具包,封装了 IO操作的相关类,使用 Commons IO 可以很方便的读写文件 commons.jar 包 ...

  8. IOUtils总结

    参考:https://www.cnblogs.com/xing901022/p/5978989.html 常用的静态变量 在IOUtils中还是有很多常用的一些变量的,比如换行符等等 public s ...

  9. 【commons-io】File对文件与目录的处理&FileUtis,IOUtils,FilenameUtils工具的使用

    -------------------File的使用-------------- 1.File类对文件的处理 1.1目录结构:  1.2测试对文件Test.txt处理: // 测试文件 @Test p ...

  10. Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils

    关键词:IntelliJ IDEA.Tomcat.commons-io-2.5.jar.java.lang.ClassNotFoundException: org.apache.commons.io. ...

随机推荐

  1. 番外:可刷新PDB的管理操作(如何切换PDB Switching Over)

    基于版本:19c (12.2.0.3) AskScuti 主题:可刷新PDB如何进行切换操作 内容说明:本篇延续如何克隆可刷新的PDB(Refreshable PDB)一文,进行切换实验. 具体请参考 ...

  2. 2020牛客寒假算法基础集训营6 I.导航系统 (最小生成树)

    https://ac.nowcoder.com/acm/contest/3007/I 题中给定的图必定是一棵树 容易发现,如果将输入的N(N-1)个距离看做N(N-1)条无向边的话,那么如果数据合法, ...

  3. 快速将Navicat中数据表信息导出

    1.使用navicat工具  2.新建查询  SELECT COLUMN_NAME 字段名, COLUMN_TYPE 数据类型, DATA_TYPE 字段类型, CHARACTER_MAXIMUM_L ...

  4. session的到底是做什么的?

    原文地址:https://blog.csdn.net/h19910518/article/details/79348051 前言: 今天就来彻底的学一些session是个啥东西,我罗列了几个需要知道的 ...

  5. Linux线程间同步的几种方式

    信号量 信号量强调的是线程(或进程)间的同步:"信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候,就阻塞 ...

  6. 关于jquery绑定事件执行两次

    经常会出现jquery绑定事件执行两次的情况,如: $("a[tag=slide]").click(function () { alert(1); $(this).parent() ...

  7. 矩阵快速幂+二分 poj3233

    #include <iostream> #include <cstdio> #include <string> #include <cstring> # ...

  8. Nearest Interesting Number

    Polycarp knows that if the sum of the digits of a number is divisible by 33, then the number itself ...

  9. Docker on startup: “No activity detected on VM, aborting”

    windows下安装的docker,切换到linux,一直处于重启中,最后报No activity detected on VM, aborting错误 上网百度一下这个错误,基本上没人遇到过,最后在 ...

  10. Js将字符串转换成对象或数组en

    举个例子 var test='{ colkey: "col", colsinfo: "NameList" }' a.将文本转换成对象 var test='{ c ...