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. javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

    这个错误的原因是没有项目使用到了Tomcat中配置的数据源(但是你本地没有配置),关于什么是JNDI看这篇文章就够了® 今天导入一个项目(比较老的),在本地运行时报错: Cannot resolve ...

  2. Java世界最常用的工具类库

    Apache Commons Apache Commons有很多子项目,常用的项目如下 BeanUtils 提供了一系列对java bean的操作,读取和设置属性值等 map和bean的互相转换 我们 ...

  3. hibernate跟Mybatis/ ibatis 的区别,为什么选择?(转)

    第一章 Hibernate与MyBatisHibernate 是当前最流行的O/R mapping框架,它出身于sf.NET,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀的O/R ...

  4. go语言 RSA数字签名和验证签名

    package main import ( "crypto" "crypto/rand" "crypto/rsa" "crypto ...

  5. Codeforces Round #603 (Div. 2) C.Everyone is A Winner!

    tag里有二分,非常的神奇,我用暴力做的,等下去看看二分的题解 但是那个数组的大小是我瞎开的,但是居然没有问题233 #include <cstdio> #include <cmat ...

  6. jsp连接数据库增删改查

    一,创建表 二.将jar包复制导入到lib文件夹下 三.创建工具包连接数据库 package com.bill.util; import java.sql.Connection; import jav ...

  7. 注解 - @Deprecated

    意思是说此方法已过时,过时的原因就是有新的API的类替代了次方法. 这个被划去的方法仍然是可以正常使用的,就是一个提示而已. Java内在的File类中有如下方法 @Deprecated public ...

  8. Linux内核源码阅读记录一之分析存储在不同段中的函数调用过程

    在写驱动的过程中,对于入口函数与出口函数我们会用一句话来修饰他们:module_init与module_exit,那会什么经过修饰后,内核就能狗调用我们编写的入口函数与出口函数呢?下面就来分析内核调用 ...

  9. select出来的表增加递增列

    Select identity(int,1,1) as no,* into #tmp from table

  10. 第四十七篇 入门机器学习——分类的准确性(Accuracy)

    No.1. 通常情况下,直接将训练得到的模型应用于真实环境中,可能会存在很多问题 No.2. 比较好的解决方法是,将原始数据中的大部分用于训练数据,而留出少部分数据用于测试,即,将数据集切分成训练数据 ...