【问题标题】:Loading text file onto MySQL - skip first row将文本文件加载到 MySQL - 跳过第一行
【发布时间】:2017-04-21 21:10:02
【问题描述】:

我正在将文本文件的内容加载到 MySQL 数据库中。我已经正确加载了表格,但我无法跳过第一行,它只是表格标题。

这是我编写的代码,考虑了该线程上的所有 cmets。我添加了一行

if($. == 1) { 

在我打开文件以跳过文本文件的标题后,但它似乎仍然无法正常工作。

感谢大家的耐心等待。

有什么建议吗?

#!/usr/bin/perl -w

use DBI;
use strict;
use Data::Dumper;

my $user = shift @ARGV or die $!;
my $password = shift @ARGV or die $!;
my $database = shift @ARGV or die $!;
my $recipient_ewes = shift @ARGV or die $!;


my $dbh = DBI->connect("DBI:mysql:$database:localhost",
                   $user,
                   $password,
                   {RaiseError => 1}
                   );



open (FILE, "rid.txt") or die $!;

while (<FILE>) {

    if($. == 1) {
        next;
    }

    my $GID;
    my $RID;
    my $number;

    my $line = $_;
    chomp $line;

    my @array = split("\t",$line);

    if (scalar(@array)==4){
        $RID = $array[0];
        $GID = $array[2];
        $number = $array[3];

        my $sth = $dbh-> prepare (qq{insert into $recipient_ewes (GID, RID,     numbertransferred) values ("$GID", "$RID", "$number")});

        $sth -> execute ();
        $sth -> finish ();
    }
}

$dbh->disconnt ();
exit;
close FILE;

输入文件是:

Recipient ID            Round            Group #            # of transfers  
6507                      1                 2                     4  
5101                      1                 4                     4  
5007                      1                 5                     3  
6535                      2                 6                     4  
6510                      2                 7                     4  

【问题讨论】:

  • next if $. == 1; 紧跟在while (...) { 之后
  • 不幸的是,这不起作用,我尝试将其放在多个位置。该命令应该做什么?
  • 它确实有效,但没有minimal reproducible example,就无法进一步帮助您。
  • 您可以阅读 perl 的特殊变量,例如 $. here$. 包含当前文件句柄的行号。此页面还将告诉您为什么不应该使用$1$2 等。在页面上搜索$&lt;digits&gt;。我同意@MattJacob 的观点,如果您需要更多帮助,您需要编辑您的问题以获得可用的代码块。
  • 您似乎正试图将数据插入到名为$recipient 的表中,但您没有声明这样的变量;只有哈希%recipient

标签: mysql perl


【解决方案1】:

@Matt Jacob 的评论将解决您的问题。

$. 给出文件的当前行号。 Here about next

while (<$fh>)
{
   if($. == 1)  #@Matt Jacob written in one line
   {
      next;
   }

   # do your stuff

}

始终在程序顶部添加use warnings;use strict;

文件句柄使用三个参数。

然后不要使用$1 $2 $3 作为变量名,这些将用于正则表达式分组。

最后以下将帮助您解决问题

open my $fh, "<", "Filename.txt";
<$fh>; #Here removing the first line

while (my $line = <$fh>)
{
   chomp $line;
   ..
   do you stuff..

【讨论】:

    【解决方案2】:

    忽略数据库的东西,读取文件时如何跳过第一行:

    use strict;
    use warnings;
    
    while (<DATA>) {
        next if $. == 1;
        my ($RID, undef, $GID, $number) = split;
        print join(', ', $RID, $GID, $number), "\n";
    }
    
    __DATA__
    Recipient ID            Round            Group #            # of transfers
    6507                      1                 2                     4
    5101                      1                 4                     4
    5007                      1                 5                     3
    6535                      2                 6                     4
    6510                      2                 7                     4
    

    输出:

    6507, 2, 4
    5101, 4, 4
    5007, 5, 3
    6535, 6, 4
    6510, 7, 4
    

    注意事项:

    • $. 是一个特殊变量,它引用最后访问的文件句柄的当前行号。
    • split 在省略模式或单个空格字符时具有特殊的空格模式。

    【讨论】:

      猜你喜欢
      • 2017-03-04
      • 1970-01-01
      • 2014-11-23
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      相关资源
      最近更新 更多