输出文件为空

时间:2019-01-15 17:09:40

标签: perl

我不知道为什么以下代码输出空白文件。我正在运行Windows8。我已经从社区获得了有关perl帮助的帮助,而这正是我能够与获得的帮助放在一起的地方。但是,我不知道该脚本在哪里出错。

#! perl
use strict;
use warnings;
use HTML::FormatText;
use LWP::Simple;

my $dateField = 'date=2018-12-31';
my $currency  = "USD";
my $filename = 'C:\Users\Name\Desktop\doc3.txt';
my $address = "https://www.oanda.com/rates/api/v1/rates/$currency.csv?api_key=gpolonsky&decimal_places=4&$dateField&fields=averages&quote=USD&quote=AUD&quote=BRL&quote=CAD&quote=GBP&quote=CHF&quote=CNY&quote=DKK&quote=HKD&quote=INR&quote=IDR&quote=ILS&quote=JPY&quote=MXN&quote=NOK&quote=PHP&quote=PLN&quote=SGD&quote=sKK&quote=ZAR&quote=KRW&quote=SEK&quote=TWD&quote=THB&quote=EUR&quote=MYR&quote=NZD&quote=SAR&quote=TRY&quote=RUB&quote=CZK&quote=AED&quote=CLP&quote=EGP&quote=MAD&quote=NGN&quote=OMR&quote=QAR";
my $content = get($address);
my $s = 1;

defined $content or die "Cannot read '$address': $!";
$content =~  s/^[^\n]*\n//s; 
$content =~  s/^/20181231,/gm; # The m option makes ^ match at the start of each line
$content =~  s/,2018-12-3[0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\+0000*//g;
$content =~  s/([A-Z][A-Z][A-Z]),[0-9]*\.[0-9][0-9][0-9][0-9]/$1/g;

my $string = HTML::FormatText->format_string(
    $content,
    leftmargin  =>  0,
    rightmargin => 23,
);

open(my $file, '>', $filename) or die $!;
print $file $string;

my @curOrd = qw( USD AUD BRL GBP CAD CNY DKK HKD INR IDR ILS JPY MXN NOK PHP PLN SGD SKK ZAR KRW SEK CHF TWD THB EUR MYR NZD SAR TRY RUB CZK AED CLP EGP MAD NGN OMR QAR );
my %data;
my $infile = 'C:\Users\Name\Desktop\doc3.txt';
my $outfile = 'file2.txt';

open (my $in, "<", $infile ) || die "can't open $infile file"; 
while (my $line = <$in>) {
    push(@{$data{(split ',', $line)[2]}}, $line);
} 
print $infile;
close $in;

open (my $out, ">", $outfile) || die "can't open $outfile file"; 
foreach my $curr (@curOrd) {
    foreach my $line (@{$data{$curr}}) {
        print $out $line;
    }
}
close $out;

2 个答案:

答案 0 :(得分:5)

从服务器获取数据并将其写入文件后,需要关闭$ file:

open(my $file, '>', $filename) or die $!;
print $file $string;
close($file)  # Add this line

我还必须在$ address中使用http://而不是https://,但是我在一个CentOS机器上,该机器安装了旧的LWP,但似乎没有执行TLS。

答案 1 :(得分:4)

您的问题是您需要明确close()要写入的文件,然后才能重新打开该文件以进行读取。

但是整个中间文件毫无意义(as I've suggested before)。只需处理内存中的所有数据,然后再将其写入输出文件即可。

use strict;
use warnings;
use feature 'say';

use LWP::Simple;

my $dateField = 'date=2018-12-31';
my $currency  = "USD";
my $filename = 'doc3.txt';
my $address = "https://www.oanda.com/rates/api/v1/rates/$currency.csv?"
            . "api_key=gpolonsky&decimal_places=4&$dateField&fields=averages&"
            . "quote=USD&quote=AUD&quote=BRL&quote=CAD&quote=GBP&"
            . "quote=CHF&quote=CNY&quote=DKK&quote=HKD&quote=INR&"
            . "quote=IDR&quote=ILS&quote=JPY&quote=MXN&quote=NOK&"
            . "quote=PHP&quote=PLN&quote=SGD&quote=sKK&quote=ZAR&"
            . "quote=KRW&quote=SEK&quote=TWD&quote=THB&quote=EUR&"
            . "quote=MYR&quote=NZD&quote=SAR&quote=TRY&quote=RUB&"
            . "quote=CZK&quote=AED&quote=CLP&quote=EGP&quote=MAD&"
            . "quote=NGN&quote=OMR&quote=QAR";

my $content = get($address);

# I've removed $! from the die() error below. $! is the last
# *operating system* error. And making an HTTP request is not
# an operating system operation - so the contents will have no
# connection to the results of our GET request.
defined $content or die "Cannot read '$address'";
$content =~  s/^[^\n]*\n//s;
$content =~  s/^/20181231,/gm; # The m option makes ^ match at the start of each line
$content =~  s/,2018-12-3[0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\+0000*//g;
$content =~  s/([A-Z][A-Z][A-Z]),[0-9]*\.[0-9][0-9][0-9][0-9]/$1/g;

my @curOrd = qw( USD AUD BRL GBP CAD CNY DKK HKD INR IDR ILS JPY MXN
                 NOK PHP PLN SGD SKK ZAR KRW SEK CHF TWD THB EUR MYR
                 NZD SAR TRY RUB CZK AED CLP EGP MAD NGN OMR QAR );

my $i = 1;
my %curr_lookup = map { $_ => $i++ } @curOrd;

my @data = map { [ split /,/ ] } split /\n/, $content;

my $outfile = 'file2.txt';

@data = sort { $curr_lookup{$a->[2]} <=> $curr_lookup{$b->[2]} }
        @data;

open my $out, '>', $outfile or die "Can't open $outfile for writing: $!";

say $out join ',', @$_ for @data;