字符串 CSV解析 表格 逗号分隔值 通讯录 电话簿 MD
Markdown版本笔记 | 我的GitHub首页 | 我的博客 | 我的微信 | 我的邮箱 |
---|---|---|---|---|
MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
字符串 CSV解析 表格 逗号分隔值 通讯录 电话簿 MD
目录
CSV文件简介
逗号分隔值:Comma-Separated Values
,CSV,有时也称为字符分隔值
,因为分隔字符也可以不是逗号。
逗号分隔值文件以纯文本形式存储表格数据
。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。
逗号分隔值:Comma-Separated Values
,CSV,有时也称为字符分隔值
,因为分隔字符也可以不是逗号。
逗号分隔值文件以纯文本形式存储表格数据
。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。
CSV文件由任意数目的记录
组成,记录间以某种换行符
分隔;每条记录由字段
组成,字段间的分隔符
是其它字符或字符串,最常见的是逗号或制表符
。
通常,所有记录都有完全相同的字段序列。通常都是纯文本文件。建议使用记事本
来开启,再则先另存新档后用EXCEL开启,也是方法之一。
CSV是一种通用的、相对简单的文件格式,被用户、商业和科学广泛应用。最广泛的应用是在程序之间转移表格数据
,而这些程序本身是在不兼容的格式上进行操作的(往往是私有的和/或无规范的格式)。因为大量程序都支持某种CSV变体,至少是作为一种可选择的输入/输出
格式。
CSV文件格式的通用标准并不存在,但是在
RFC 4180
中有基础性的描述。使用的字符编码同样没有被指定,但是7-bitASCII
是最基本的通用编码。
CSV并不是一种单一的、定义明确的格式,在实践中,术语 CSV 泛指具有以下特征的任何文件:
- 纯文本,使用某个字符集,比如ASCII、Unicode、EBCDIC或GB2312;
- 由记录组成(典型的是每行一条记录);
- 每条记录被分隔符分隔为字段(典型分隔符有逗号、分号或制表符;有时分隔符可以包括可选的空格);
- 每条记录都有同样的字段序列。
在这些常规的约束条件下,存在着许多CSV变体,故CSV文件
并不完全互通
。然而,这些变异非常小,并且有许多应用程序允许用户预览
文件(这是可行的,因为它是纯文本),然后指定分隔符、转义规则
等。如果一个特定CSV文件的变异过大,超出了特定接收程序的支持范围,那么可行的做法往往是人工检查并编辑文件
,或通过简单的程序来修复问题。因此在实践中,CSV文件还是非常方便的。
解析工具类
数据格式:
微信号,bqt20094,这里是密码,邮箱#909120849@qq.com,QQ#909120849,
工具类
//解析方式之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvUtils {
private static final String COMMA = ",";
private static final String SEPARATOR = "#";
private static final String LINE_SEPARATOR = File.separator;
private static final String ENCODING = "GBK";
public static void obj2CsvFils(List<CsvBean> dataList, File file) {
String content = obj2String(dataList);
writeFile(content, file);
}
public static List<CsvBean> csvFils2Obj(String filePath) {
String content = readFile(filePath);
return string2Obj(content);
}
private static String obj2String(List<CsvBean> dataList) {
StringBuilder sb = new StringBuilder();
for (CsvBean cvsBean : dataList) {
sb.append(cvsBean.name).append(COMMA).append(cvsBean.account).append(COMMA);
if (isNotEmpty(cvsBean.password)) sb.append(cvsBean.password);//密码有可能为空
sb.append(COMMA);
if (cvsBean.other != null && !cvsBean.other.keySet().isEmpty()) {
for (String key : cvsBean.other.keySet()) {
String value = cvsBean.other.get(key);
if (isNotEmpty(value)) sb.append(key).append(SEPARATOR).append(value).append(COMMA);
}
}
sb.append(LINE_SEPARATOR);
}
return sb.toString();
}
private static void writeFile(String content, File file) {
try {
FileOutputStream writer = new FileOutputStream(file);
writer.write(content.getBytes(ENCODING));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<CsvBean> string2Obj(String content) {
if (content == null) return null;
String[] array = content.split(LINE_SEPARATOR);
List<CsvBean> list = new ArrayList<CsvBean>();
for (String string : array) {
int index = string.indexOf(COMMA);
if (index >= 0) {
CsvBean bean = new CsvBean();
bean.name = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.account = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.password = string.substring(0, index);
string = string.substring(index + 1);
for (int i = 0; i <= string.length(); i++) {
if (string.endsWith(",")) string = string.substring(0, string.length() - 1);
else break;
}
if (string.length() > 0) {
String[] otherStrings = string.split(COMMA);
bean.other = new HashMap<>();
for (String other : otherStrings) {
String[] keyValue = other.split(SEPARATOR);
if (keyValue.length >= 2) {
bean.other.put(keyValue[0], keyValue[1]);
}
}
}
} else {
bean.password = string;
}
} else {
bean.account = string;
}
list.add(bean);
}
}
return list;
}
private static String readFile(String filePath) {
File file = new File(filePath);
byte[] temp = new byte[(int) file.length()];
try {
FileInputStream in = new FileInputStream(file);
in.read(temp);
in.close();
return new String(temp, ENCODING);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static boolean isNotEmpty(String string) {
return string != null && string.trim().length() > 0 && !string.equalsIgnoreCase("null");
}
}
数据模型
//数据结构之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvBean {
public String name;//必选项
public String account;//必选项
public String password;//可选项
public HashMap<String, String> other;//可选项
@Override
public String toString() {
return "CvsBean [name=" + name + ", account=" + account + ", password=" + password + ", other=" + other + "]";
}
}
微信号,bqt20094,这里是密码,邮箱#909120849@qq.com,QQ#909120849,
//解析方式之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvUtils {
private static final String COMMA = ",";
private static final String SEPARATOR = "#";
private static final String LINE_SEPARATOR = File.separator;
private static final String ENCODING = "GBK";
public static void obj2CsvFils(List<CsvBean> dataList, File file) {
String content = obj2String(dataList);
writeFile(content, file);
}
public static List<CsvBean> csvFils2Obj(String filePath) {
String content = readFile(filePath);
return string2Obj(content);
}
private static String obj2String(List<CsvBean> dataList) {
StringBuilder sb = new StringBuilder();
for (CsvBean cvsBean : dataList) {
sb.append(cvsBean.name).append(COMMA).append(cvsBean.account).append(COMMA);
if (isNotEmpty(cvsBean.password)) sb.append(cvsBean.password);//密码有可能为空
sb.append(COMMA);
if (cvsBean.other != null && !cvsBean.other.keySet().isEmpty()) {
for (String key : cvsBean.other.keySet()) {
String value = cvsBean.other.get(key);
if (isNotEmpty(value)) sb.append(key).append(SEPARATOR).append(value).append(COMMA);
}
}
sb.append(LINE_SEPARATOR);
}
return sb.toString();
}
private static void writeFile(String content, File file) {
try {
FileOutputStream writer = new FileOutputStream(file);
writer.write(content.getBytes(ENCODING));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<CsvBean> string2Obj(String content) {
if (content == null) return null;
String[] array = content.split(LINE_SEPARATOR);
List<CsvBean> list = new ArrayList<CsvBean>();
for (String string : array) {
int index = string.indexOf(COMMA);
if (index >= 0) {
CsvBean bean = new CsvBean();
bean.name = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.account = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.password = string.substring(0, index);
string = string.substring(index + 1);
for (int i = 0; i <= string.length(); i++) {
if (string.endsWith(",")) string = string.substring(0, string.length() - 1);
else break;
}
if (string.length() > 0) {
String[] otherStrings = string.split(COMMA);
bean.other = new HashMap<>();
for (String other : otherStrings) {
String[] keyValue = other.split(SEPARATOR);
if (keyValue.length >= 2) {
bean.other.put(keyValue[0], keyValue[1]);
}
}
}
} else {
bean.password = string;
}
} else {
bean.account = string;
}
list.add(bean);
}
}
return list;
}
private static String readFile(String filePath) {
File file = new File(filePath);
byte[] temp = new byte[(int) file.length()];
try {
FileInputStream in = new FileInputStream(file);
in.read(temp);
in.close();
return new String(temp, ENCODING);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static boolean isNotEmpty(String string) {
return string != null && string.trim().length() > 0 && !string.equalsIgnoreCase("null");
}
}
//数据结构之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvBean {
public String name;//必选项
public String account;//必选项
public String password;//可选项
public HashMap<String, String> other;//可选项
@Override
public String toString() {
return "CvsBean [name=" + name + ", account=" + account + ", password=" + password + ", other=" + other + "]";
}
}
2018-9-8
字符串 CSV解析 表格 逗号分隔值 通讯录 电话簿 MD的更多相关文章
- 利用FastJson,拼接复杂嵌套json数据&&直接从json字符串中(不依赖实体类)解析出键值对
1.拼接复杂嵌套json FastJson工具包中有两主要的类: JSONObject和JSONArray ,前者表示json对象,后者表示json数组.他们两者都能添加Object类型的对象,但是J ...
- (转)csv — 逗号分隔值文件格式
原文:https://pythoncaff.com/docs/pymotw/csv-comma-separated-value-files/125 csv 模块主要用于处理从电子数据表格或数据库中导入 ...
- C#对.CSV格式的文件--逗号分隔值文件 的读写操作及上传ftp服务器操作方法总结
前言 公司最近开发需要将数据保存到.csv文件(逗号分隔值 文件)中然后上传到ftp服务器上,供我们系统还有客户系统调用,之前完全没有接触过这个,所以先来看看百度的解释:逗号分隔值(Comma-Sep ...
- CSV (逗号分隔值文件格式)
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本).纯文本意味着该文件是一个字符序列,不 ...
- LINQ to XML 从逗号分隔值 (CSV) 文件生成 XML 文件
参考:http://msdn.microsoft.com/zh-cn/library/bb387090.aspx 本示例演示如何使用 语言集成查询 (LINQ) 和 LINQ to XML 从逗号分隔 ...
- jsoncpp封装和解析字符串、数字、布尔值和数组
使用jsoncpp进行字符串.数字.布尔值和数组的封装与解析. 1)下载jsoncpp的代码库 百度网盘地址 :http://pan.baidu.com/s/1ntqQhIT 2)解压缩文件 json ...
- [原创]SQL表值函数:把用逗号分隔的字符串转换成表格数据
我们日常开发过程中,非常常见的一种需求,把某一个用逗号或者/或者其他符号作为间隔的字符串分隔成一张表数据. 在前面我们介绍了 [原创]SQL 把表中字段存储的逗号隔开内容转换成列表形式,当然按照这 ...
- CSV.js – 用于 CSV 解析和编码的 JS 工具库
逗号分隔值(CSV )文件用于以以纯文本的形式存储表格化数据(数字和文本). CSV 文件包含任意数量的记录,通过某种换行符分隔,每条记录由字段,其他一些字符或字符串分隔,最常用的是文字逗号或制表符. ...
- 支持各种特殊字符的 CSV 解析类 (.net 实现)(C#读写CSV文件)
CSV是一种十分简洁的数据结构,在DOTNET平台实际使用中发现微软官方并没有提供默认的方法,而网上好多例子发现实现并不严谨甚至一些含有明显错误,所以后面自己实现了一个读写工具类,这里发出来希望方便后 ...
随机推荐
- android 四大组件
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 活动,服务,广播接受者,内容提供者. 活动 能够提供 用户界面.服务 没有用户界面.广 ...
- Codeforces.643E.Bear and Destroying Subtrees(DP 期望)
题目链接 \(Description\) 有一棵树.Limak可以攻击树上的某棵子树,然后这棵子树上的每条边有\(\frac{1}{2}\)的概率消失.定义 若攻击以\(x\)为根的子树,高度\(ht ...
- HICON泄漏
通常,我们使用的HICON对象只需用DestroyIcon后就不存在内存泄漏了,但是当我们使用GetIconInfo后会发现程序GDI资源存在泄漏,原因是GetIconInfo会产生2个HBITMAP ...
- ios网络请求
1.AFNetworking object 2.Alamofire swift
- SlickMaster.NET 开源表单设计器快速使用指南
前言:在企业数据处理过程中,经常需要通过定制表单来输入业务数据.由于涉及的数据比较离散,并不同于ERP系统的紧密关联数据.假如由开发人员每个增加页面,工作量会比较大,后期后期的维护很升级也耗费时间和精 ...
- 将Azure WebSite(应用)集成到虚拟网络
用过Azure的同学都知道如何将虚拟机加入虚拟网络,也非常简单,但是对于将应用(WebSite)集成到虚拟网络中却很少听到.今天我要讲的就是如何直接将我们部署在WebSite中的应用加入到虚拟网络. ...
- Python包管理工具pip安装
Python版本在2.7.9+以上的都自带pip无需安装,但在CentOS 7里面自带的Python是2.7.5,所以需要单独安装. 安装: curl https://bootstrap.pypa.i ...
- HDU 4123 Bob’s Race(树形DP,rmq)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- STM32的PWM输入模式设置并用DMA接收数据
参考 :STM32输入捕获模式设置并用DMA接收数据 PWM input mode This mode is a particular case of input capture mode. The ...
- Synchronize Ultimate
支持多种服务器和主流云网盘进行同步 http://www.icecoldapps.com/ Unlock Code : xda201506 Unlock Code : icecoldapps20150 ...