ioutils
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的更多相关文章
- apache.commons.io.IOUtils: 一个很方便的IO工具库(比如InputStream转String)
转换InputStream到String, 比如 //引入apache的io包 import org.apache.commons.io.IOUtils; ... ...String str = IO ...
- Java程序员的日常—— IOUtils总结
以前写文件的复制很麻烦,需要各种输入流,然后读取line,输出到输出流...其实apache.commons.io里面提供了输入流输出流的常用工具方法,非常方便.下面就结合源码,看看IOUTils都有 ...
- IO - IOUtils
Commons IO is a library of utilities to assist with developing IO functionality. There are four main ...
- Commons IO - IOUtils
IOUtils is a general IO stream manipulation utilities. This class provides static utility methods fo ...
- 文件输入输出流工具: IOUtils使用总结
序言 以前写文件的复制很麻烦,需要各种输入流,然后读取line,输出到输出流...其实apache.commons.io里面提供了输入流输出流的常用工具方法,非常方便.下面就结合源码,看看IOUTil ...
- Delphi 2010 新增功能之: IOUtils 单元(6): TPath(结构体) 的方法与属性
以后路径相关的处理, 用 IOUtils.TPath 就很方便了. //较常用的方法: TPath.GetTempPath; {获取临时文件夹路径} TPath.Ge ...
- Apache IOUtils的使用
IOUtils 与 FileUtilsCommons IO 是 apache 的一个开源的工具包,封装了 IO操作的相关类,使用 Commons IO 可以很方便的读写文件 commons.jar 包 ...
- IOUtils总结
参考:https://www.cnblogs.com/xing901022/p/5978989.html 常用的静态变量 在IOUtils中还是有很多常用的一些变量的,比如换行符等等 public s ...
- 【commons-io】File对文件与目录的处理&FileUtis,IOUtils,FilenameUtils工具的使用
-------------------File的使用-------------- 1.File类对文件的处理 1.1目录结构: 1.2测试对文件Test.txt处理: // 测试文件 @Test p ...
- 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. ...
随机推荐
- webpack 之搭建本地服务器
搭建本地服务器 webpack提供了一个可选的本地开发服务器,这个本地服务器基于node.js搭建,内部使用express框架,可以实现 我们想要的让浏览器自动刷新显示我们修改后的结果 不过它是一个单 ...
- 计蒜客 - A1633.蒜头君的数轴
我感觉出的很好的一道题,首先不难想到(其实我刚开始没想到),加点的个数就是找已有点两两形成区间的gcd,那么问题就出在了复杂度上,每次循环哪个区间不要复杂度过高,所以运用正反两次前缀和(?好像不能这么 ...
- CF399B Red and Blue Balls
题目 CF399B 洛谷RemoteJudge 思路 很容易发现,栈中靠上的蓝色球的出栈,对它下方的蓝色球没有影响. 举个例子: 第一步中靠上的蓝色球在第三步出栈了,这一过程对它下面的蓝色球(即第一步 ...
- 2019牛客多校第五场B generator 十进制快速幂
generator 1 题意 给出\(x_0,x_1,a,b\)已知递推式\(x_i=a*x_{i-1}+b*x_{i-2}\),出个n和mod,求\(x_n\) (n特别大) 分析 比赛的时候失了智 ...
- Wannafly Camp 2020 Day 3C 无向图定向
请你把无向图的每条边确定一个方向,使之成为一个DAG,并且最小化最长路的长度. #include <bits/stdc++.h> using namespace std; int n,m, ...
- SVN提交时没有写注释
会报错: Error: Commit blocked by pre-commit hook (exit code 1) with output: Error: [Commit failed]: Emp ...
- js时间相关操作
取值 this.divEndDate.attr("value",new Date()); var date = new Date(); date.getYear(): 取2 ...
- tp5.0和tp3.2的区别
1. 控制器输出return $this->fetch(); ----5$this->display(); ----3.2 单字母函数去掉了 如:M() D() U() S() C() 3 ...
- Spring AOP操作action时无法注入,报NullPointer异常
Spring AOP操作action时无法注入,报NullPointer异常当使用Spring AOP对action层进行操作时,会出现注入失败的问题,出现空指针异常.原因是一般struts2+spr ...
- vue的$on,$emit
使用 $on(eventName) 监听事件使用 $emit(eventName) 触发事件 Api 中的解释: vm.$emit( event, […args] ) 参数: {string} eve ...