Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用csv_xs perl删除引号之间的逗号和引号字符_Regex_Perl - Fatal编程技术网

Regex 使用csv_xs perl删除引号之间的逗号和引号字符

Regex 使用csv_xs perl删除引号之间的逗号和引号字符,regex,perl,Regex,Perl,我想就如何删除引用文件中的COMA和引号提出建议 例: 美国广播公司,山姆,汤姆,15岁 在上面的示例中,对于值abc,应删除;对于sam,应删除;对于tom,应删除。我正在CSV_XS模块中使用以下代码,但它不起作用 #!/usr/bin/perl -w # use strict; use Text::CSV_XS; # Read the input filename from the command line my $file = shift or die "Usage: $0 <c

我想就如何删除引用文件中的COMA和引号提出建议

例: 美国广播公司,山姆,汤姆,15岁

在上面的示例中,对于值abc,应删除;对于sam,应删除;对于tom,应删除。我正在CSV_XS模块中使用以下代码,但它不起作用

#!/usr/bin/perl -w
#
use strict;
use Text::CSV_XS;

# Read the input filename from the command line
my $file = shift or die "Usage: $0 <csv_filename>\n";

# instantiate the CSV parser
my $csv = Text::CSV_XS->new ({ binary => 1 }, escape_char         => '"',allow_loose_quotes  => 1); 

# open the input file for read
open my $inputFH,  "<", $file or die "$file: $!";

# open the output file for write
open my $outputFH, ">", "$file.out" or die "$file.out: $!";

my @out;             # declare variables outside the loops for better performance
my $outputRow;
my $inputRow;
while ($inputRow = $csv->getline($inputFH)) { # iterate over each row in the input file
  @out=();           # empty out the array which will hold our corrected fields
  foreach (@$inputRow) {   # iterate over each field in the input row
    s/[\0\|\n\r]//g;         # get rid NUL, pipe, CR, and LF characters
    s/\s+/ /g;               # change multi-whitespace to single
    push(@out,$_);         # push the corrected field on to an array
    print "Loop"
  }
  $outputRow = join('|',@out);  # create a pipe-delimited line from the corrected array

  $outputRow =~ s/^\s+//;  # trim leading whitespace
  $outputRow =~ s/\s+$//;  # trim trailing whitespace

  print $outputFH "$outputRow\n";
}

文件说,它应该能够通过将allow_loose_quotes设置为1,将escape_char设置为除此之外的任何值来处理严重损坏的CSV