本文介绍了如何使用正则表达式解析Perl中的引用的CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用引号解析CSV数据时遇到一些问题。我的主要问题是一个字段中的引号。在以下示例中,第1 - 4行正常工作,但第5,6和7行不正常。

  COLLOQ_TYPE,COLLOQ_NAME,COLLOQ_CODE, XDATA 
S,BELT,FAN,003541547,
S,BELT V,FAN,000324244,
S,SHROUD SPRING SCREW,000868265,
S,D REL VALVE ASSY,000771881,
S,YBELT,V,000323030,
S,YBELT,'V',000322933,



我想避免Text :: CSV因为它未安装在目标服务器上。意识到CSV是比他们看起来更复杂的我从Perl Cookbook中使用一个食谱。

  sub parse_csv {
my $ text = shift; #record containg CSVs
my @columns =();
push(@columns,$ +)while $ text =〜m {
#第一部分将引号内的短语分组
([^ \\\] * ?:\\。[^ \\\] *)*),?
| ([^,] +),?
| ,
} gx;
push(@columns,undef)if substr($ text,-1,1)eq',';
return @columns; #以逗号分隔的vars列表。
}

有没有人建议改进正则表达式来处理上述情况? / p>

解决方案

请尝试使用CPAN



t下载或任何其他非基于XS的实施的副本的CSV解析器,并将其安装在您的本地目录中,或者在您的项目的lib /子目录中,以便与您的项目一起安装。



t在你的项目中存储文本文件,那么我想知道你是如何编码你的项目。





应该是如何使这些工具进入工作状态的良好指南本地。



不使用CPAN确实是一个灾难。



请在考虑这一点之前,实施。



Text :: CSV 是超过一百行代码,包括固定的错误和边缘情况,并从头重写这将只是让你学会如何可怕的CSV可以是困难的方式。




I'm having some issues with parsing CSV data with quotes. My main problem is with quotes within a field. In the following example lines 1 - 4 work correctly but 5,6 and 7 don't.

COLLOQ_TYPE,COLLOQ_NAME,COLLOQ_CODE,XDATA
S,"BELT,FAN",003541547,
S,"BELT V,FAN",000324244,
S,SHROUD SPRING SCREW,000868265,
S,"D" REL VALVE ASSY,000771881,
S,"YBELT,"V"",000323030,
S,"YBELT,'V'",000322933,

I'd like to avoid Text::CSV as it isn't installed on the target server. Realising that CSV's are are more complicated than they look I'm using a recipe from the Perl Cookbook.

sub parse_csv {
  my $text = shift; #record containg CSVs
  my @columns = ();
  push(@columns ,$+) while $text =~ m{
    # The first part groups the phrase inside quotes
    "([^\"\\]*(?:\\.[^\"\\]*)*)",?
      | ([^,]+),?
      | ,
    }gx;
  push(@columns ,undef) if substr($text, -1,1) eq ',';
  return @columns ; # list of vars that was comma separated.
}

Does anyone have a suggestion for improving the regex to handle the above cases?

解决方案

Please, Try Using CPAN

There's no reason you couldn't download a copy of Text::CSV, or any other non-XS based implementation of a CSV parser and install it in your local directory, or in a lib/ sub directory of your project so its installed along with your projects rollout.

If you can't store text files in your project, then I'm wondering how it is you are coding your project.

http://novosial.org/perl/life-with-cpan/non-root/

Should be a good guide on how to get these into a working state locally.

Not using CPAN is really a recipe for disaster.

Please consider this before trying to write your own CSV implementation.

Text::CSV is over a hundred lines of code, including fixed bugs and edge cases, and re-writing this from scratch will just make you learn how awful CSV can be the hard way.

这篇关于如何使用正则表达式解析Perl中的引用的CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:52