如何根据第一列修改 CSV 文件的第二列?

发布于 2024-10-20 07:10:27 字数 1907 浏览 1 评论 0原文

我是 Perl 新手,我有一个包含电子邮件和姓名的 CSV 文件,如下所示:

[email protected];John
[email protected];
[email protected];Richard
[email protected];
[email protected];Andrew

但是,正如您所看到的,一些条目/行包含电子邮件地址和 ;字段分隔符,但缺少名称。我需要逐行阅读,如果名称字段丢失,我想在此位置打印电子邮件的开头,直到@domainX.com。输出示例:

[email protected];John
[email protected];Paul
[email protected];Richard
[email protected];Rob
[email protected];Andrew

我是 Perl 新手,我进行了逐行读取的迭代,如下所示:

#!/usr/bin/perl
use warnings;
use strict;

open (MYFILE, 'test.txt');
while (<MYFILE>) {
    chomp;
}

但我无法解析条目以使用 ; 作为分隔符并检查是否名称字段丢失,因此打印电子邮件的开头而不包含域。

有人可以根据我的代码给我一个例子吗?

I'm new to Perl and I have a CSV file that contains e-mails and names, like this:

[email protected];John
[email protected];
[email protected];Richard
[email protected];
[email protected];Andrew

However, as you can see a few entries/lines have the e-mail address and the ; field separator, but lack the name. I need to read line by line and and if the name field is missing, I want to print in this place the begin of the e-mail until @domainX.com. Output example:

[email protected];John
[email protected];Paul
[email protected];Richard
[email protected];Rob
[email protected];Andrew

I'm new with Perl, I did the iteration of read line by line, such this:

#!/usr/bin/perl
use warnings;
use strict;

open (MYFILE, 'test.txt');
while (<MYFILE>) {
    chomp;
}

But I'm failing to parse the entries to use ; as a separator and to check if the name field is missing and consequently print the begin of the e-mail without the domain.

Can someone please give me a example based on my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羞稚 2024-10-27 07:10:27

首先,如果文件可能包含真实 CSV(或在您的情况下为空格SV)数据(例如带引号的字段),我强烈建议使用标准Perl 模块来解析它。

否则,一个简单的例子可以是:

#!/usr/bin/perl

use warnings;
use strict;

# In modern Perl, please always use 3-aqr form of open and lexical filehandles.
# More robust
open $fh, "<", 'test.txt' || die "Can not open: $!\n";

while (<$fh>) {
    chomp;
    my ($email, name) = split(/;/, $_);
    if (!$name) {
        my ($userid, $domain) = split(/\@/, $email);
        $name = $userid;
    }
    print "$space_prefix$email;$name\n"; # Print to STDOUT for simplicity of example
}
close($fh);

First, if the file may contain real CSV (or space SV in your case) data (e.g. quoted fields), I'd strongly recommend using a standard Perl module to parse it.

Otherwise, a quick-and-dirty example can be:

#!/usr/bin/perl

use warnings;
use strict;

# In modern Perl, please always use 3-aqr form of open and lexical filehandles.
# More robust
open $fh, "<", 'test.txt' || die "Can not open: $!\n";

while (<$fh>) {
    chomp;
    my ($email, name) = split(/;/, $_);
    if (!$name) {
        my ($userid, $domain) = split(/\@/, $email);
        $name = $userid;
    }
    print "$space_prefix$email;$name\n"; # Print to STDOUT for simplicity of example
}
close($fh);
谢绝鈎搭 2024-10-27 07:10:27

尝试:

#!/usr/bin/env perl

use strict;
use warnings;

for my $file ( @ARGV ){

  open my$in_fh, '<', $file or die "could not open $file: $!\n";

  while( my $line = <$in_fh> ){
    chomp( $line );

    my ( $email, $name ) = split m{ \; }msx, $line;
    if( ! ( defined $name && length( $name ) > 0 ) ){
      ( $name ) = split m{ \@ }msx, $email;
      $name = ucfirst( lc( $name ));
    }

    print "$email;$name\n";
  }
}

Try:

#!/usr/bin/env perl

use strict;
use warnings;

for my $file ( @ARGV ){

  open my$in_fh, '<', $file or die "could not open $file: $!\n";

  while( my $line = <$in_fh> ){
    chomp( $line );

    my ( $email, $name ) = split m{ \; }msx, $line;
    if( ! ( defined $name && length( $name ) > 0 ) ){
      ( $name ) = split m{ \@ }msx, $email;
      $name = ucfirst( lc( $name ));
    }

    print "$email;$name\n";
  }
}
箜明 2024-10-27 07:10:27

我不是珍珠程序员,但我会首先在空格字符上进行拆分,然后您可以迭代结果并按分号进行拆分。然后,您可以检查分号分割数组的第二个成员,如果它为空,则将其替换为分号分割数组的第一个成员的开头。然后,只需颠倒该过程,首先用分号连接,然后用空格连接。

I am not a pearl programmer, but I would split first on the space character, and then you could iterate through the results and split by the semi-colon. Then you can check the second member of the semi-colon split array, and if it is empty, replace it with the beginning of the first member of the semi-colon split array. Then, just reverse the process, first joining by semi-colons and then by spaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文