如何不让Tie::Handle::CSV 在字符串外加双引号""?

7 views
Skip to first unread message

许高飞

unread,
Mar 10, 2011, 3:58:49 AM3/10/11
to perl...@googlegroups.com
请教一下大家,我在用Tie::Handle::CSV模块处理CSV文件时,发现其输出时会在字符串外加双引号""。
例如:
FOOBAR, Name, Type,
输出会变成:
FOOBAR," Name"," Type"," "
有什么办法不让Tie::Handle::CSV 在字符串外加双引号""吗?
代码例子见附件。

test.zip

Robin Lee

unread,
Mar 10, 2011, 4:39:37 AM3/10/11
to perl...@googlegroups.com
因为有空格,所以加双引号是规范的做法,试试写成
FOOBAR,Name,Type,
也就是逗号后不加空格
2011/3/10 许高飞 <flyh...@gmail.com>:
> --
> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
> 要向此网上论坛发帖,请发送电子邮件至 perl...@googlegroups.com
> 要取消订阅此网上论坛,请发送电子邮件至 perlchina+...@googlegroups.com
> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>

Shu Cao

unread,
Mar 10, 2011, 5:01:39 AM3/10/11
to perl...@googlegroups.com
改成如下:

use Text::CSV_XS;

...

my $csv_parser = Text::CSV_XS->new( {allow_whitespace => 1} );
my $fh_input = Tie::Handle::CSV->new("$inputfile", header => 1,
csv_parser => $csv_parser);

...

2011/3/10 许高飞 <flyh...@gmail.com>:

> --
> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
> 要向此网上论坛发帖,请发送电子邮件至 perl...@googlegroups.com
> 要取消订阅此网上论坛,请发送电子邮件至 perlchina+...@googlegroups.com
> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>

--
Shu Cao

Godfrey Xu

unread,
Mar 15, 2011, 10:14:27 AM3/15/11
to PerlChina Mongers 讨论组
谢谢,不过 "allow_whitespace = 1" 会把头尾的空白字符去掉,有什么办法把这些空白字符也保留下来吗?

allow_whitespace
When this option is set to true, whitespace (TAB's and
SPACE's)
surrounding the separation character is removed when parsing.

On Mar 10, 6:01 pm, Shu Cao <shu...@gmail.com> wrote:
> 改成如下:
>
> use Text::CSV_XS;
>
> ...
>
> my $csv_parser = Text::CSV_XS->new( {allow_whitespace => 1} );
> my $fh_input = Tie::Handle::CSV->new("$inputfile", header => 1,
> csv_parser => $csv_parser);
>
> ...
>

> 2011/3/10 许高飞 <flyhig...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages