avatar
请教:parse CSV文件# Java - 爪哇娇娃
F*e
1
这个文件是MySQL dump出来的CSV, 所以每个column都是逗号(,)分开的,问题是有
一个column是blob, dump出来是text, text里本身含有逗号,在文中显示 \, 比如说这
一行:
1,2,a\, b,4
一共应该是4个columns, 如果用String.split(",")的话,就把当中的text给拆掉了。
应该输出是
1
2
a, b
4
怎么搞啊?包子答谢! //bow
avatar
Z*e
2
简单方法:现改dump文件的结构,把blob放在最后一个column,然后用split(regex,
limit)函数限制最多能分出来的列数

【在 F**e 的大作中提到】
: 这个文件是MySQL dump出来的CSV, 所以每个column都是逗号(,)分开的,问题是有
: 一个column是blob, dump出来是text, text里本身含有逗号,在文中显示 \, 比如说这
: 一行:
: 1,2,a\, b,4
: 一共应该是4个columns, 如果用String.split(",")的话,就把当中的text给拆掉了。
: 应该输出是
: 1
: 2
: a, b
: 4

avatar
g*g
3
A simple solution, replace "\," with a special string which won't occur,
e.g "__temp__", split, then replace "__temp__" with "," one by one.
If you want good performance, use StringTokenizer

【在 F**e 的大作中提到】
: 这个文件是MySQL dump出来的CSV, 所以每个column都是逗号(,)分开的,问题是有
: 一个column是blob, dump出来是text, text里本身含有逗号,在文中显示 \, 比如说这
: 一行:
: 1,2,a\, b,4
: 一共应该是4个columns, 如果用String.split(",")的话,就把当中的text给拆掉了。
: 应该输出是
: 1
: 2
: a, b
: 4

avatar
b*y
4
I guess you should dump to TAB separated file, if possible. To keep it
simple and easy for ya.
avatar
c*t
5
The presence of \, might indicate that \\ or other escape sequences.
Couple with error detection, it is a pain to write a perfect parser
by hand.
(btw, I think the standard CSV uses double quote for literals. It
is a bit strange to use \,)
Java's regular expression is not particularly efficient.
The best way is to use a lex tool (like my CookCC which is for Java)
to deal with this situation.

【在 g*****g 的大作中提到】
: A simple solution, replace "\," with a special string which won't occur,
: e.g "__temp__", split, then replace "__temp__" with "," one by one.
: If you want good performance, use StringTokenizer

avatar
g*y
6
input.replaceAll("(?
avatar
F*e
7
多谢各位!包子已发。
暂时用的是goodbug's simple solution. 自己觉得这个办法有点笨,但想想我这里
performance没什么concern, 先这样用吧,等其他搞完了再回来找找其他办法,也试试
楼上的一些建议。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。