[英]How can I parse CSV data with internal commas in data using Perl?
The data I need to parse looks like: 我需要解析的数据如下所示:
[fild1, filed2, .... filedn] , [filed1, filed2, .... filedn] .....
I call it a special form of CSV data because there are two kinds of comma: 我将其称为CSV数据的一种特殊形式,因为它有两种逗号:
So in this case using split(',' , $data)
will not serve my needs which is to parse the data and get every record in the data. 因此,在这种情况下,使用
split(',' , $data)
不能满足我的需要,即解析数据并获取数据中的每条记录。
This should do the job: 这应该做的工作:
my @out = map{[split/,/]} $data =~ /\[([^\]]+)\]/g;
example: 例:
use Data::Dumper;
$data='[1,2,3],[4,5],[6]';
@a=map{[split/,/]} $data =~ /\[([^\]]+)\]/g;
print Dumper @a;
output: 输出:
$VAR1 = [
'1',
'2',
'3'
];
$VAR2 = [
'4',
'5'
];
$VAR3 = [
'6'
];
How about: my @parts = split(/\\]/, $data);
怎么样:
my @parts = split(/\\]/, $data);
, and then you can iterate over @parts
, remove the heading [
and split once more by "," ,然后可以遍历
@parts
,删除标题[
并再次用“,”分割
You can also make the initial split like so: my @parts = split(/\\] , /, $data);
您也可以像这样进行初始拆分:
my @parts = split(/\\] , /, $data);
and that will save you some more cleanup later. 这将为您节省以后的清理工作。 Just be sure to only use this method if your data's whitespaces are consistent.
只要确保仅在数据的空白一致的情况下使用此方法即可。
my @a = split /\]\s*,\s*\[/, $data;
并删除第一个'['和最后一个']'。
这是一个简单的示例,假定$data
中的值有效。
my @data = map { [ split ',', $_ ] } $data =~ / \[ ([^\[\]]*) \] ,? /xg;
you can also try out Text::CSV or Text::CSV_XS. 您也可以尝试使用Text :: CSV或Text :: CSV_XS。 go to CPAN to download.
前往CPAN下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.