如何使用Perl创建CSV文件?

时间:2021-06-14 09:23:03

I want to create a CSV file using Perl and write some data to it. Is there any way to do that?

我想使用Perl创建一个CSV文件并向其写入一些数据。有没有办法做到这一点?

3 个解决方案

#1


25  

You could use Class:CSV.

您可以使用Class:CSV。

use Class::CSV;

my $csv = Class::CSV->new(
  fields         => [qw/userid username/]
);

$csv->add_line([2063, 'testuser']);
$csv->add_line({
  userid   => 2064,
  username => 'testuser2'
});

$csv->print();

# 2063,testuser
# 2064,testuser2

Edit: For more libraries, you can search CPAN.

编辑:对于更多库,您可以搜索CPAN。

#2


17  

We usually use Text::CSV_XS (which the above-mentioned Class::CSV is based on)

我们通常使用Text :: CSV_XS(上面提到的Class :: CSV基于)

UPDATE: Commenters below also suggest using Text::CSV which will load up Text::CSV_XS or, if that's not avialable, fall back on Text::CSV_PP that doesn't have XS dependency and may be easier to install.

更新:下面的评论者还建议使用Text :: CSV,它将加载Text :: CSV_XS,或者,如果不可用,则回退到没有XS依赖性的Text :: CSV_PP,可能更容易安装。

#3


11  

There is more than one way to do it.

有不止一种方法可以做到这一点。

  • DBD::CSV - Use SQL to read and write CSV files.
  • DBD :: CSV - 使用SQL读取和写入CSV文件。
  • Text::CSV - Build and parse CSV files a line at a time. This is pretty much the - gold standard for CSV manipulation.
  • Text :: CSV - 一次构建和解析一行CSV文件。这几乎是CSV操作的黄金标准。
  • POE::Filter::CSV - Provides a CSV filter for your POE component IO.
  • POE :: Filter :: CSV - 为POE组件IO提供CSV过滤器。
  • Data::Tabular::Dumper::CSV - Dump a table directly into a CSV file (objects with the same interface can dump to XML or MS Excel files).
  • Data :: Tabular :: Dumper :: CSV - 将表直接转储到CSV文件中(具有相同接口的对象可以转储到XML或MS Excel文件)。

There are many others on CPAN, too.

CPAN上还有很多其他的。

Of course, you could ignore all this and just use a loop, a join and a print:

当然,您可以忽略所有这些,只需使用循环,连接和打印:

my @table = (
  [ 'a', 'b', 'c'],
  [  1,   2,   3 ],
  [  2,   4,   6 ],
);

for my $row ( @table ) {
    print join( ',', @$row ), "\n";
}

Just run your script and redirect output to a file, bang! you are done. Or you could get crazy and open a file handle, and print directly to a file.

只需运行您的脚本并将输出重定向到文件,砰!你完成了或者您可能会疯狂并打开文件句柄,并直接打印到文件。

But then you won't handle multi-line fields, or embedded commas, or any of the many other oddities that can pop up in this sort of data processing task. So be careful with this approach.

但是,您将无法处理多行字段,嵌入式逗号,或任何可以在此类数据处理任务中弹出的任何奇怪的东西。所以要小心这种方法。

My recommendations:

我的建议:

  • If you are building a POE application, use POE::Filter.
  • 如果要构建POE应用程序,请使用POE :: Filter。
  • If you are a SQL/DBI monster and dream in SQL, or may need to replace CSV output with a real database connection, use DBD::SQL.
  • 如果您是SQL / DBI怪物并且梦想在SQL中,或者可能需要用真实数据库连接替换CSV输出,请使用DBD :: SQL。
  • If you have simple data and are cobbling together a cruddy little throw-away script to format some data for your spreadsheet to digest, use print and join--do this for no code with an expected life span of greater than 2 hours.
  • 如果你有简单的数据,并且正在拼凑一个肮脏的小丢弃脚本来格式化你的电子表格要消化的数据,请使用print和join - 这样做的代码是预期寿命超过2小时的代码。
  • If you want to dump a blob of data into a CSV or XML use Data::Tabular::Dumper::CSV.
  • 如果要将数据blob转储为CSV或XML,请使用Data :: Tabular :: Dumper :: CSV。
  • If you are writing something that needs to be stable, maintainable and fast, and you need maximum control for input and output, use Text::CSV. (Note that POE::Filter::CSV, Data::Tabular::Dumper::CSV, and DBD::CSV all use Text::CSV or Text::CSV_XS as for back-end processing).
  • 如果您正在编写需要稳定,可维护且快速的内容,并且需要对输入和输出进行最大程度的控制,请使用Text :: CSV。 (注意,POE :: Filter :: CSV,Data :: Tabular :: Dumper :: CSV和DBD :: CSV都使用Text :: CSV或Text :: CSV_XS作为后端处理)。

#1


25  

You could use Class:CSV.

您可以使用Class:CSV。

use Class::CSV;

my $csv = Class::CSV->new(
  fields         => [qw/userid username/]
);

$csv->add_line([2063, 'testuser']);
$csv->add_line({
  userid   => 2064,
  username => 'testuser2'
});

$csv->print();

# 2063,testuser
# 2064,testuser2

Edit: For more libraries, you can search CPAN.

编辑:对于更多库,您可以搜索CPAN。

#2


17  

We usually use Text::CSV_XS (which the above-mentioned Class::CSV is based on)

我们通常使用Text :: CSV_XS(上面提到的Class :: CSV基于)

UPDATE: Commenters below also suggest using Text::CSV which will load up Text::CSV_XS or, if that's not avialable, fall back on Text::CSV_PP that doesn't have XS dependency and may be easier to install.

更新:下面的评论者还建议使用Text :: CSV,它将加载Text :: CSV_XS,或者,如果不可用,则回退到没有XS依赖性的Text :: CSV_PP,可能更容易安装。

#3


11  

There is more than one way to do it.

有不止一种方法可以做到这一点。

  • DBD::CSV - Use SQL to read and write CSV files.
  • DBD :: CSV - 使用SQL读取和写入CSV文件。
  • Text::CSV - Build and parse CSV files a line at a time. This is pretty much the - gold standard for CSV manipulation.
  • Text :: CSV - 一次构建和解析一行CSV文件。这几乎是CSV操作的黄金标准。
  • POE::Filter::CSV - Provides a CSV filter for your POE component IO.
  • POE :: Filter :: CSV - 为POE组件IO提供CSV过滤器。
  • Data::Tabular::Dumper::CSV - Dump a table directly into a CSV file (objects with the same interface can dump to XML or MS Excel files).
  • Data :: Tabular :: Dumper :: CSV - 将表直接转储到CSV文件中(具有相同接口的对象可以转储到XML或MS Excel文件)。

There are many others on CPAN, too.

CPAN上还有很多其他的。

Of course, you could ignore all this and just use a loop, a join and a print:

当然,您可以忽略所有这些,只需使用循环,连接和打印:

my @table = (
  [ 'a', 'b', 'c'],
  [  1,   2,   3 ],
  [  2,   4,   6 ],
);

for my $row ( @table ) {
    print join( ',', @$row ), "\n";
}

Just run your script and redirect output to a file, bang! you are done. Or you could get crazy and open a file handle, and print directly to a file.

只需运行您的脚本并将输出重定向到文件,砰!你完成了或者您可能会疯狂并打开文件句柄,并直接打印到文件。

But then you won't handle multi-line fields, or embedded commas, or any of the many other oddities that can pop up in this sort of data processing task. So be careful with this approach.

但是,您将无法处理多行字段,嵌入式逗号,或任何可以在此类数据处理任务中弹出的任何奇怪的东西。所以要小心这种方法。

My recommendations:

我的建议:

  • If you are building a POE application, use POE::Filter.
  • 如果要构建POE应用程序,请使用POE :: Filter。
  • If you are a SQL/DBI monster and dream in SQL, or may need to replace CSV output with a real database connection, use DBD::SQL.
  • 如果您是SQL / DBI怪物并且梦想在SQL中,或者可能需要用真实数据库连接替换CSV输出,请使用DBD :: SQL。
  • If you have simple data and are cobbling together a cruddy little throw-away script to format some data for your spreadsheet to digest, use print and join--do this for no code with an expected life span of greater than 2 hours.
  • 如果你有简单的数据,并且正在拼凑一个肮脏的小丢弃脚本来格式化你的电子表格要消化的数据,请使用print和join - 这样做的代码是预期寿命超过2小时的代码。
  • If you want to dump a blob of data into a CSV or XML use Data::Tabular::Dumper::CSV.
  • 如果要将数据blob转储为CSV或XML,请使用Data :: Tabular :: Dumper :: CSV。
  • If you are writing something that needs to be stable, maintainable and fast, and you need maximum control for input and output, use Text::CSV. (Note that POE::Filter::CSV, Data::Tabular::Dumper::CSV, and DBD::CSV all use Text::CSV or Text::CSV_XS as for back-end processing).
  • 如果您正在编写需要稳定,可维护且快速的内容,并且需要对输入和输出进行最大程度的控制,请使用Text :: CSV。 (注意,POE :: Filter :: CSV,Data :: Tabular :: Dumper :: CSV和DBD :: CSV都使用Text :: CSV或Text :: CSV_XS作为后端处理)。