【问题标题】:Reading line from file with double double quotes [closed]从带有双双引号的文件中读取行[关闭]
【发布时间】:2014-09-17 09:39:41
【问题描述】:

在 perl 中读取双引号行的最佳方法是什么? 例如,

            " This is ""an example"" of double double quotes" 

我需要阅读这一行并执行一些操作并将其保存回文件中。 如果我尝试读取此文件,Getline 将失败。 有没有更好的方法来读取这个文件并执行逐行操作?

【问题讨论】:

  • “Getline 失败”是什么意思? Getline 是什么,它是怎么失败的???请演示问题。

标签: perl quotes getline


【解决方案1】:

Perl 中没有 getline。你说的可能是IO->getline()

嗯……

文件 test.txt:

" This is ""an example"" of double double quotes" 

Perl 程序:

#! /usr/bin/env perl
use warnings;
use strict;
use feature qw(say);
use autodie;

say "Using builtin Perl file operations.";
open my $fh, "<", "test.txt";
while ( my $line = <$fh> ) {
    chomp $line;
    say "The line is <$line>";
}
close $fh;

say "Using IO::File in order to use 'getline'.";
use IO::File;
my $io = IO::File->new;
$io->open("test.txt");
while ( my $line = $io->getline ) {
    chomp $line;
    say "The line is <$line>";
}

打印出来:

$ ./test.pl
Using builtin Perl file operations.
The line is <" This is ""an example"" of double double quotes" >
Using IO::File in order to use 'getline'.
The line is <" This is ""an example"" of double double quotes" >

使用内置的 Pler 文件方法或使用来自 IO::Filegetline 读取带有多个引号的行绝对没有问题。

您能更具体地说明您的经历吗?你想做什么?你的代码是什么?

你说的是 Perl 还是 Python?

【讨论】:

  • 这可能是一个CSV文件,在这种情况下他应该使用Text::CSV_XS,而不是这个。
  • 我也是这么认为的,但是他作为示例给出的行不是您在 CVS 文件中看到的行。也许这是一个领域?谁知道。我希望 OP 会发布更多信息。
  • 请找出失败的代码段 smy $csvFile = new Text::CSV_XS({eol=>"\n", sep_char=>",", binary=>1, escape_char=> " \"", quote_char=> "\""});以及失败的getline:$Rec_ref = $csvFile -> getline($file) or &zdie($MSG_PASSTHRU,"Unable to read line in $l_FileName :: $!\n");
  • 我看不懂。编辑您的问题并将其放入问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 2014-04-22
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多