【问题标题】:Open file only returning last line of file打开文件只返回文件的最后一行
【发布时间】:2014-02-05 03:29:57
【问题描述】:

我在整个程序中多次对不同的文件使用同样的代码,没有问题:

#if the info is part of a file
if($proteinIn =~  m/\.txt$/i){
    my $input_file = catfile('..', dataset => $proteinIn);

    open my $protein_file, '<', $input_file
        or die "couldn't open '$input_file': $!";
    while (my $protLine = <$protein_file>) {
        print $protLine."\n";
        $protLine =~ s/\s+\z//; # remove all trailing space
        $protein{$protLine} = 1;
    }
    close $protein_file;
}

读取包含此内容的文件时

Q5KDZ7_CRYNJ
Q2U9C0_ASPOR
Q2U048_ASPOR
G2Q3M9_THIHA
G2QAZ2_THIHA

打印语句 "print $protLine."\n";"只打印最后一行。在这种情况下:

G2QAZ2_THIHA

我的程序中的另一个示例读取给定文件的每一行

foreach my $tempFile(@fileList){
    my $input_file = catfile('..', dataset => $tempFile);

    open my $ps_file, '<', $input_file
        or die "couldn't open '$input_file': $!";
    while (my $line = <$ps_file>) {
        $line =~ s/\s+\z//; # remove all trailing space

        my @curLine = split /\t/, $line;
<the rest of the program>

此代码打开的文件包含如下行:

>sp|Q6GZX4|001R_FRG3G Putative transcription factor 001R OS=Frog virus 3 (isolate Goorha) GN=FV3-001R PE=4 SV=1 MAFSAEDVLKEYDRRRRMEALLLSLYYPNDRKLLDYKEWSPPRVQVECPKAPVEWNNPPSEKGLIVGHFSGIKYKGEKAQASEVDVNKMCCWVSKFKDAMRRYQGIQTCKIPGKVLSDLDAKIKAYNLTVEGVEGFVRYSRVTKQHVAAFLKELRHSKQYENVNLIHYILTDKRVDIQHLEKDLVKDFKALVESAHRMRQGHMINVKYILYQLLKKHGHGPDGPDILTVKTGSKGVLYDDSFRKIYTDLGWKFTPL    FRG3G

为什么第一个示例代码只打印文件的最后一行?

编辑:对代码上游问题的评论的回应;幸运的是,我的问题即将开始,所以这里是问题代码之前的所有内容

运行代码时,我使用了命令“perl regProt.pl”“truePool.txt”“uniprot_sprot.dat”“fungi”

#!/usr/bin/env perl
use strict;
use File::Spec::Functions qw( catfile );

#use warnings;
#@author David Dziak

#A program for quick regex functions on uniprot data to test protein signatures
#my $max = 325783;
#my $cur = 0;
my $annotation;
my $fingerprint = $ARGV[0];
unless($fingerprint){
    $fingerprint = "[GASRK][KRVSG][RKVI][KRVI]x[ASCR]x[AST]x(0,45)[GATF]xxx[VLI]N[GKND]x(11,12)[RKL]x(16,18)[NDA]x(6)[GS]GGx(10)[AG][LIVM][GAS][KR][GASN][VLI]";#prosite s9

    #$fingerprint = "[GS]Gx(2)[GSA][QK]x(2)[SA]x(3)[GSA]x[GSTAV][KR][GSALVD][LIFV]";#prosite s9
    #$fingerprint = "[STDNQ]G[KRNQMHSI]x(6)[LIVM]x(4)[LIVMC][GSD]x(2)[LFI][GAS][DE][FYM]x(2)[ST]";#prosite s19
    #$fingerprint = "[RKHN][KSTR]X(3)[AVSCR]X(6)GXGX(0,23)X(25)GGGX(2)[GAS][QRKS]X(0,50)X(20)[APS]RX(5)[VSTA]XR";#s9
    #$fingerprint = "[GAR][RKHG][RKHNT][KSTR]X(3)[AVSCR][RASTHKQLP]X(5)[GPSTND]X[GPSTKDQ]X(4)[NDVGIT]X(0,60)G[GS]GX(2)[GSA][QRS]X(0,70)[QTRA][FWYETK][STAVH][KY][RK]";#s9
    #$fingerprint = "[RGWCKT]X(5)PX(3)[GARDENS]X(4)[VIL][HYF]XGX(7)[LIVMP]X(7)x[LFI][GASR][DEA][FYME]";#s19
}
$annotation .= $fingerprint;
#protein name to search
my $proteinIn = $ARGV[1];
my %protein;
#if the info is part of a file
if($proteinIn =~  m/\.txt$/i){
    my $input_file = catfile('..', dataset => $proteinIn);

【问题讨论】:

  • 您的示例工作正常,在您显示的代码之前必须有其他内容。或者你读了一个不同的文件。
  • @squiguy: catfile 大概来自 File::Spec::Functions
  • 我在问题区域之前添加了代码。我的电脑上只有一个文件本身在一行上有“G2QAZ2_THIHA”。这是我要打开的文件 其他文件可能包含“G2QAZ2_THIHA”,但它总是在同一行有其他文本
  • 使用od 告诉我们文件实际包含的内容。我敢打赌它有 CR 而不是 LF 作为行尾。
  • 下一次,将问题减少到重现问题所需的最少代码和数据,每行不应超过 5 行。

标签: perl file eof


【解决方案1】:

我打赌你的文件出于某种原因有由\r 分隔的行,而不是\n

尝试放在顶部:

use Data::Dumper;
$Data::Dumper::Useqq=1;

并将打印替换为:

print Data::Dumper::Dumper($protLine);

【讨论】:

  • 这就是问题所在。我早该知道;这是我第二次因为那些该死的 /rs 而不得不在这里发帖。 $line =~ s/\s+\z//;当 chomp() 不起作用时,是对最后一个的修复。
猜你喜欢
  • 1970-01-01
  • 2023-01-15
  • 2016-01-19
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多