Perl從文字檔案中按關鍵字提取資料
#require "convertword.pl";
use strict;
use warnings;
use Cwd;
use File::Spec::Functions qw( catfile );
# 轉換格式
print "轉換Word為文字檔案...\n";
#&convertWord();
`perl convertword.pl`;
# 關鍵字設定
#my @KEY_WORD = ("申請部門","申請時間","申請人");
my @KEY_WORD = ();
# 獲取當前目錄的完整路徑資訊。
my $currentDir = getcwd;
print "當前目錄: ".$currentDir."\n";
# 配置檔案
my $cfgFile = "$currentDir/關鍵字.txt";
# 設定原始檔夾
my $SRC_PATH = "$currentDir/TXT/";
# 設定臨時資料夾
my $TMP_PATH = "$currentDir/Temp/";
# 設定目標資料夾
my $DEST_PATH="$currentDir/Result/";
# 如果目標資料夾不存在,則建立資料夾
mkdir( $DEST_PATH, 0777 ) if ( !-d $DEST_PATH);
opendir TEMP, ${SRC_PATH} or die "無法開啟".$SRC_PATH."目錄,請檢查一下目錄是否存在!";
@KEY_WORD=&readConfig();
# 讀取目錄下所有檔案
my @filelist = readdir TEMP;
my $srcFile='';
my $srcFileBackup='';
my $destFile=$DEST_PATH.'Result.csv';
my $dateflag=0;
my $ownerflag=0;
my $date='';
my $owner='';
my $count=0;
my $flag=0;
my $line='';
# 如果目標資料夾不存在,則建立資料夾
mkdir($DEST_PATH, 0777) if(! -d $DEST_PATH);
# 刪除並新建臨時目錄
deldir($TMP_PATH) if( -d $TMP_PATH);
mkdir($TMP_PATH, 0777) if(! -d $TMP_PATH);
open(FH_PS, ">$destFile") or die "無法開啟檔案: $destFile <$!>";
# 輸出檔案頭
my $header = (join ',', @KEY_WORD).",";
print FH_PS $header."\n";
foreach (@filelist) {
next if /^\./;
$count=$count+1;
$srcFile=$SRC_PATH.$_;
$srcFileBackup=$TMP_PATH.$_.'.bkp';
print "\n";
open(FH_B,">$srcFileBackup") or die "無法開啟檔案: $srcFileBackup";
open(FH,$srcFile) or die "無法開啟檔案: $srcFile";
while(<FH>){
#chomp;
s/\r/\|/g;
s/\n\n/\n/g;
#s/\n\n/\n/g;
print FH_B $_;
#s/提交人\r/\n提交人|/;
#s/\r提交單位\r/\n提交單位|/;
}
close(FH_B);
close(FH);
open(FH_B,$srcFileBackup) or die "無法開啟檔案: $srcFileBackup";
my $result='';
while(<FH_B>){
chomp;
$line = $_;
foreach my $key(@KEY_WORD){
# 根據關鍵字匹配相應的值
if($line=~/$key\|(.+?)\|(.+)/){
my $value = $1;
print $count.":".$key."|".$value."\n";
$result = $result.$value.",";
}
}
}
print FH_PS $result."\n";
close(FH_B);
}
close(FH_PS);
# 刪除臨時目錄
deldir($TMP_PATH) if( -d $TMP_PATH);
print "All files are processed.\nTotal file processed: $count\n";
sub readConfig(){
# 讀取配件檔案 - START
open(FH_CONFIG, "<$cfgFile") || die "無法開啟配置檔案 : $cfgFile <$!>";
my @words=();
while(<FH_CONFIG>) {
chomp;
next if /^#/; # skip comments
next if /^\s*$/; # skip empty lines
if (/^\s*(.+),(.+)\s*$/){
@words = split (/,/,$_);
last;
}
}
print "@words\n";
close FH_CONFIG;
trimList(@words);
# 讀取配件檔案 - END
}
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Perl trim function to remove whitespace from the start and end of the string
sub trimList($)
{
my @myList = ();
foreach(@_){
push (@myList, trim($_));
}
return @myList;
}
sub deldir {
my($del_dir)=$_[0];
my(@direct);
my(@files);
opendir (DIR2,"$del_dir");
my(@allfile)=readdir(DIR2);
close (DIR2);
foreach (@allfile){
if (-d "$del_dir/$_"){
push(@direct,"$_");
}
else {
push(@files,"$_");
}
}
my
my [email protected];
if ($files ne "0"){
foreach (@files){
unlink("$del_dir/$_");
}
}
if ($direct ne "0"){
foreach (@direct){
&deldir("$del_dir/$_") if($_ ne "." && $_ ne "..");
}
}
rmdir ("$del_dir");
}
相關推薦
Perl從文字檔案中按關鍵字提取資料
#!/usr/bin/perl -w #require "convertword.pl"; use strict; use warnings; use Cwd; use File::Spec::Functions qw( catfile ); # 轉換格式 print "轉
《機器學習實戰》第2章閱讀筆記3 使用K近鄰演算法改進約會網站的配對效果—分步驟詳細講解1——資料準備:從文字檔案中解析資料(附詳細程式碼及註釋)
本篇使用的資料存放在文字檔案datingTestSet2.txt中,每個樣本資料佔據一行,總共有1000行。 樣本主要包含以下3中特徵: (1)每年獲得飛行常客里程數 (2)玩視訊遊戲所耗時間百分比 (3)每週消費的冰淇淋公升數 在使用分類器之前,需要將處理的檔案格式
從文字檔案中讀取資料到集合中,並遍歷集合
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; /* * 需求:從文字檔案中讀取資料(每一行
iOS開發程式碼:從文字檔案中讀取內容到字串裡(+ .json檔案)
NSError *error; NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”myTextFile” ofType:@”txt”]
Python從txt檔案中逐行讀取資料
Python從txt檔案中逐行讀取資料 # -*-coding:utf-8-*- import os for line in open("./samples/label_val.txt"): print('line=', line, end = '') #後面
另類爬蟲:從PDF檔案中爬取表格資料
簡介 本文將展示一個稍微不一樣點的爬蟲。 以往我們的爬蟲都是從網路上爬取資料,因為網頁一般用HTML,CSS,JavaScript程式碼寫成,因此,有大量成熟的技術來爬取網頁中的各種資料。這次,我們需要爬取的文件為PDF檔案。本文將展示如何利用Python的camelot模組
從PCD檔案中讀取點雲資料(Reading Point Cloud data from PCD files)
在本教程中,我們將學習如何從PCD檔案中讀取點雲資料。 #程式碼 首先,在你最喜歡的編輯器中建立一個名為pcd_read.cpp的檔案,並在其中放置下面的程式碼: #include <iostream> #include <pcl/io/pcd
從主檔案中讀取n個子資料夾中的圖片(每一子檔案中有兩個資料夾,每兩個檔案中有18張圖片)
tic clc; clear all; close all; str = 'F:\shijie-test\test-finger\'; pt = dir(str); foldname = []; k = 0; for i = 1 : length(pt) ifstr
js-linq.js 從json字串中按條件查詢資料
Linq.js作為js的一大框架還是蠻強大的。因為要從json物件中提取出自己需要的資料資訊,同事推薦了linq.js。真心不錯,就和sql語句一樣根據自己條件進行查詢資料。不過百度這方面的資料有點少。 匯入的js: <script src="static/jq
如何用Python從PDF檔案中提取文字詞彙
在日常工作中,有時可能需要解析一些 PDF 檔案,提取檔案中的關鍵詞,好讓它們能夠被我們搜尋。解決這個問題的重要部分就是找到如何從 PDF 檔案中提取文字資料的方法。從如果是幾張或者幾十張倒還好辦,那要是幾百幾千張,可能就有點麻煩了。 幸好我們可以用 Python 完成這項工作。下面就分享
從類似如下的文字檔案中讀取出所有的姓名,並打印出重複的姓名和重複的次數,按重複次數排序: (程式設計, 提示:集合)
import java.io.BufferedReader;import java.io.FileInputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.TreeSet;public clas
從zip壓縮包中提取檔案中的關鍵字
本文目的是從zip壓縮包中獲取各個檔案中內容,然後從中查詢關鍵字,將包含關鍵字的檔案目錄打印出來。 package com.spider.readzip; import java.io.BufferedInputStream; import java.io.Buffere
用mplayer從視頻中按周期提取幀
-s 處理程序 文件中 大於 style 程序 圖片 周期性 dex 使用方法:extract file time step folder time 設置時間長度 step 設置周期 均以秒(s)為單位 貢獻:1. 從視頻文件中周期性提取圖片;2. Windo
從一段視頻中按幀提取圖片
imp book ima open HA image %d 數據 使用 這裏實現的是從一段視頻中每10幀讀取第10幀圖片,代碼如下: # -*- coding:utf-8 -*- #視頻中按序列提取幀,獲得訓練數據 import cv2 import os
[SimplePlayer] 4. 從視訊檔案中提取音訊
提取音訊,具體點來說就是提取音訊幀。提取方法與從視訊檔案中提取影象的方法基本一樣,這裡僅列出其中的不同點: 1. 由於目的提取音訊,因此在demux的時候需要指定的是提取audio stream AudioStream = av_find_best_stream(pFormatCtx, AVMEDIA_T
【小工具】——從文字內容中提取日期時間
需求 需要從文字檔案中提取中時間及日期 程式碼 “`java /** * 從文字內容中提取日期時間 * @param text 包含日期時間的文字(格式:yy
基於Zlib實現的從ZIP檔案中提取檔案資料
[cpp] view plaincopyprint? ZEXTRACT_API int GetFileInZip(CMemBuffer& buffer,constchar* zfn,constchar* fname,constchar* password){ unzFile uf = u
一行命令從 APK 檔案中提取 Endpoint 及 URL
做IoT的人免不了要接觸Android,接觸Android的人又免不了要研究別人的App應用。Diggy,一款能夠從 apk 檔案中提取 endpoint 及 URL 的工具,只要一行命令就可以幫大家提取出相關Android apk檔案的安裝資訊和網際網路訪問資訊。下載地址:
從文字內容中提取有效資訊
例如a.txt中有如下內容$ABC,eqwe,0123,N,we23,E,234$ABD,fkjd,2454,N,fwer,E,456$AB,fhew,9478,N,wewf,E,rnju$ABC,we
ORACLE 中寫入txt文字與從Txt檔案中讀入資料 修改表結構
--建立一個表 DROP TABLE TEST CASCADE CONSTRAINTS ; CREATE TABLE TEST(A VARCHAR(30),B VARCHAR(30)); --檢視具體的許可權 SELECT DISTINCT PRIVILEGE FROM D