如何根据第一列修改 CSV 文件的第二列?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,如果文件可能包含真实 CSV(或在您的情况下为空格SV)数据(例如带引号的字段),我强烈建议使用标准Perl 模块来解析它。
否则,一个简单的例子可以是:
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:
尝试:
Try:
我不是珍珠程序员,但我会首先在空格字符上进行拆分,然后您可以迭代结果并按分号进行拆分。然后,您可以检查分号分割数组的第二个成员,如果它为空,则将其替换为分号分割数组的第一个成员的开头。然后,只需颠倒该过程,首先用分号连接,然后用空格连接。
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.