亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 2316 | 回復(fù): 4
打印 上一主題 下一主題

用perl批量轉(zhuǎn)換txt為xls [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-10-31 18:24 |只看該作者 |倒序?yàn)g覽
目前面臨以下數(shù)據(jù)結(jié)構(gòu)(txt格式)
文件名:1.txt
                                       1                       2                   3
item1                             100                   200                300
item2                             300                   400                450
文件名:2.txt
                                      1                       2            
item1                             500                   600               
item2                             700                   900      
item3                             1000                 3000      
第一行是年度,但是沒(méi)有對(duì)應(yīng)的說(shuō)明。
字段、數(shù)據(jù)之間是用空格(或者制表符tab)隔開(kāi)。
相關(guān)文件共有1000多個(gè)。
要求:用perl提取相應(yīng)的年度/項(xiàng)目/數(shù)據(jù)信息,并將每個(gè)文件生成對(duì)應(yīng)的xls文件如下。
1.xls
FileName          Item                     year1                 year2              year3
    1                 item1                     100                   200                300
    1                 item2                     300                   400                450
2.xls
Filename            Item                            year1                  year2            
   2                   item1                             500                   600               
   2                   item2                             700                   900      
   2                    item3                            1000                 3000     

我是Perl的初學(xué)者,參考論壇各位的程序建議,編寫(xiě)程序如下:

#!/usr/bin/perl

use Spreadsheet::WriteExcel;
       
#Specify the directory containing the files that you want to read
my $direct="e:\\test";
#Specify the directory containing the files that you want to save
my $dirOut="e:\\test1";
#If Windows "\\", if Mac "/";
my $slash='\\';
#The following two steps open the directory containing the files you plan to read
#and then stores the name of each file in an array called @data.
opendir(DIR1,"$direct")||die "Can't open directory";
my @New1=readdir(DIR1);

foreach $file(@New1)
{
# this step removes the default end of line character (\n)
# so the the entire file can be read in at once.
local $/;
open (SLURP, "$direct$slash"."$file") or die "can't open $file: $!";
#read the contents into file
$file = <SLURP>;
}
#close the filehandle called SLURP
close SLURP or die "cannot close $file: $!";

if ($file){
my @data = map [split], <$file>;
splice $data[0], 5, 2, join '_', @{$data[0]}[5,6];

my $xls    = Spreadsheet::WriteExcel->new('"$dirOut$slash"."$file".xls');
my $ws     = $xls->add_worksheet();
my $format = $xls->add_format();
$format->set_align('right');

my $i = 0;
for my $e (@data) {
    my $j = 0;
    map $ws->write( $i, $j++, $_, $format ), @$e;
    $i++;
                   }
             }
運(yùn)行結(jié)果顯示 can't open .: Permission denied at table_test2.pl line 21
請(qǐng)各位高手指教!
多謝!

求職 : 軟件工程師
論壇徽章:
3
程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-10-07 06:20:00程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-12-13 06:20:00程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2016-05-05 06:20:00
2 [報(bào)告]
發(fā)表于 2013-10-31 19:43 |只看該作者
把 excel 文件關(guān)閉后,運(yùn)行腳本

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2013-10-31 20:59 |只看該作者
我運(yùn)行一個(gè)文件,成功。
但是用循環(huán)語(yǔ)句,做不了。
請(qǐng)教如何編寫(xiě)循環(huán)語(yǔ)句?或者我的程序哪里有問(wèn)題,望各位大俠不吝指教。

論壇徽章:
1
天蝎座
日期:2014-02-28 16:08:53
4 [報(bào)告]
發(fā)表于 2013-11-05 11:20 |只看該作者
打印下dir內(nèi)容,估計(jì)是..和.的問(wèn)題

論壇徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午馬
日期:2014-08-06 03:56:58
5 [報(bào)告]
發(fā)表于 2013-11-05 14:40 |只看該作者
用循環(huán)語(yǔ)句
  1. for $file (@New1){
  2.   Do_Everything_HERE;
  3. }
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP