当前位置:网站首页>系统运维系列 之CSV文件读取时内容中包含逗号的处理方法
系统运维系列 之CSV文件读取时内容中包含逗号的处理方法
2022-08-03 20:25:00 【琅晓琳】
问题描述:
CSV文件默认逗号分隔,但是如果在内容中包含逗号,则会导致分隔失败或者数组下标匹配越界的问题。
处理方法:
(1)正则表达式
//(?=pattern)
//非获取匹配,正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串,该匹配不需要获取供以后使用
//(?:pattern)
//非获取匹配,匹配pattern但不获取匹配结果,不进行存储供以后使用。这在使用或字符“(|)”来组合一个模式的各个部分是很有用。例如“industr(?:y|ies)”就是一个比“industry|industries”更简略的表达式
//分割没有被双引号扩住的逗号
line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")
(2)使用第三方jar
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.4</version>
</dependency>
参考资料:
https://blog.csdn.net/wls_2019/article/details/123225335 csv文件读写中的特殊情况(逗号)
https://www.cnblogs.com/allen2333/p/9835654.html 正则表达式 - (?!), (?, (?=)
https://blog.csdn.net/superbeyone/article/details/83624241 Java读取CSV文件(CSV文件数据内容包含逗号处理)
边栏推荐
猜你喜欢
随机推荐
ES6 - Arrow Functions
ES6简介及let、var、const区别
华为设备配置VRRP与BFD联动实现快速切换
leetcode 136. Numbers that appear only once (XOR!!)
消除对特权账户的依赖使用Kaniko构建镜像
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
Leetcode 16. Numerical integral power (power + fast recursive/iteration)
leetcode 1837. The sum of the digits in the K-base representation
云服务器如何安全使用本地的AD/LDAP?
Leetcode 899. An orderly queue
ES6解构赋值--数组解构及对象解构
alicloud3搭建wordpress
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
In-depth understanding of JVM-memory structure
How can a cloud server safely use local AD/LDAP?
leetcode 461. 汉明距离
Go语言为任意类型添加方法
codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
谁的孙子最多II
tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)