線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:4757
推到 Plurk!
推到 Facebook!

如何讀取文字檔中我想要的文字段落

答題得分者是:pceyes
doom834
一般會員


發表:2
回覆:4
積分:1
註冊:2006-11-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-02-21 15:27:06 IP:59.125.xxx.xxx 訂閱
各位先進大家好,小弟是Delphi的新手,所以請問一些問題
我有2個檔案如附件,若我想要擷取下最紅字部分後顯示在DateGrid上。

FILE_1
Bin01-01 Total QTY:2
LotNumber:
Date: 2007/12/27 09:04:02
M/C No: 002
Wafer Name: B0763710.csv
QTY: 2
Barcode No: F0203373

Min Avg Max Std
LOP1 060.12 060.75 061.38 000.63
VF2 003.48 003.48 003.48 000.00
WLD 454.41 454.52 454.63 000.11

FILE_2
Bin41-01 Total QTY:6998
LotNumber: 20070724-001
Date: 2007/07/30 15:44:30
M/C No: 03
Wafer Name: B0728311.csv
QTY: 98
Wafer Name: B0728305.rpt
QTY: 3291
Wafer Name: B0729711.csv
QTY: 316
Wafer Name: B0729701.csv
QTY: 1158
Wafer Name: B0726802.csv
QTY: 1895
Wafer Name: B0730306.csv
QTY: 6
Wafer Name: B0730410.csv
QTY: 236
Barcode No: F0300235

Min Avg Max Std
LOP1 120.01 123.14 137.48 002.58
VF1 001.09 002.27 002.40 000.25
WLD 462.51 464.64 464.99 000.43
IR 000.00 000.01 000.66 000.03
VF2 003.32 003.42 003.49 000.04

我遇到幾個問題如下:
  1. LotNumber有空白字串出現,我該如何做???
  2. Barcode No出現在不同檔案時不同列,我該如何做???
  3. 最後要擷取最後Min Avg Max Std此行後的資料,我希望能存成LOP1_Min=120.01、LOP1_Avg=123.14、LOP1_Max=137.48、LOP1_Std=2.58,以下的以此類推。
  4. 在不同檔案時,LOP1、VF1等等出現的順序、項目也不同。
以上希望有人能幫我解惑,讓小弟能對Delphi能有更深的了解
hotswin
中階會員


發表:72
回覆:92
積分:52
註冊:2003-11-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-02-21 15:58:06 IP:59.125.xxx.xxx 訂閱
用正則表達示來取
有一 Tregexpr 元件可以用喔

===================引 用 doom834 文 章===================
各位先進大家好,小弟是Delphi的新手,所以請問一些問題
我有2個檔案如附件,若我想要擷取下最紅字部分後顯示在DateGrid上。

FILE_1
Bin01-01 Total QTY:2
LotNumber:
Date: 2007/12/27 09:04:02
M/C No: 002
Wafer Name: B0763710.csv
QTY: 2
Barcode No: F0203373

Min Avg Max Std
LOP1 060.12 060.75 061.38 000.63
VF2 003.48 003.48 003.48 000.00
WLD 454.41 454.52 454.63 000.11

FILE_2
Bin41-01 Total QTY:6998
LotNumber: 20070724-001
Date: 2007/07/30 15:44:30
M/C No: 03
Wafer Name: B0728311.csv
QTY: 98
Wafer Name: B0728305.rpt
QTY: 3291
Wafer Name: B0729711.csv
QTY: 316
Wafer Name: B0729701.csv
QTY: 1158
Wafer Name: B0726802.csv
QTY: 1895
Wafer Name: B0730306.csv
QTY: 6
Wafer Name: B0730410.csv
QTY: 236
Barcode No: F0300235

Min Avg Max Std
LOP1 120.01 123.14 137.48 002.58
VF1 001.09 002.27 002.40 000.25
WLD 462.51 464.64 464.99 000.43
IR 000.00 000.01 000.66 000.03
VF2 003.32 003.42 003.49 000.04

我遇到幾個問題如下:
  1. LotNumber有空白字串出現,我該如何做???
  2. Barcode No出現在不同檔案時不同列,我該如何做???
  3. 最後要擷取最後Min Avg Max Std此行後的資料,我希望能存成LOP1_Min=120.01、LOP1_Avg=123.14、LOP1_Max=137.48、LOP1_Std=2.58,以下的以此類推。
  4. 在不同檔案時,LOP1、VF1等等出現的順序、項目也不同。
以上希望有人能幫我解惑,讓小弟能對Delphi能有更深的了解
------
xinjier禮品贈品
doom834
一般會員


發表:2
回覆:4
積分:1
註冊:2006-11-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-02-21 16:41:55 IP:59.125.xxx.xxx 訂閱

===================引 用 hotswin 文 章===================
用正則表達示來取
有一 Tregexpr 元件可以用喔
這位先進,我沒用過這個元件,是否能請你寫個範例讓我參考呢???

===================引 用 doom834 文 章===================
各位先進大家好,小弟是Delphi的新手,所以請問一些問題
我有2個檔案如附件,若我想要擷取下最紅字部分後顯示在DateGrid上。

FILE_1
Bin01-01 Total QTY:2
LotNumber:
Date: 2007/12/27 09:04:02
M/C No: 002
Wafer Name: B0763710.csv
QTY: 2
Barcode No: F0203373

Min Avg Max Std
LOP1 060.12 060.75 061.38 000.63
VF2 003.48 003.48 003.48 000.00
WLD 454.41 454.52 454.63 000.11

FILE_2
Bin41-01 Total QTY:6998
LotNumber: 20070724-001
Date: 2007/07/30 15:44:30
M/C No: 03
Wafer Name: B0728311.csv
QTY: 98
Wafer Name: B0728305.rpt
QTY: 3291
Wafer Name: B0729711.csv
QTY: 316
Wafer Name: B0729701.csv
QTY: 1158
Wafer Name: B0726802.csv
QTY: 1895
Wafer Name: B0730306.csv
QTY: 6
Wafer Name: B0730410.csv
QTY: 236
Barcode No: F0300235

Min Avg Max Std
LOP1 120.01 123.14 137.48 002.58
VF1 001.09 002.27 002.40 000.25
WLD 462.51 464.64 464.99 000.43
IR 000.00 000.01 000.66 000.03
VF2 003.32 003.42 003.49 000.04

我遇到幾個問題如下:
  1. LotNumber有空白字串出現,我該如何做???
  2. Barcode No出現在不同檔案時不同列,我該如何做???
  3. 最後要擷取最後Min Avg Max Std此行後的資料,我希望能存成LOP1_Min=120.01、LOP1_Avg=123.14、LOP1_Max=137.48、LOP1_Std=2.58,以下的以此類推。
  4. 在不同檔案時,LOP1、VF1等等出現的順序、項目也不同。
以上希望有人能幫我解惑,讓小弟能對Delphi能有更深的了解
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-02-21 16:42:25 IP:122.127.xxx.xxx 訂閱
          Min     Avg     Max     Std
LOP1 120.01 123.14 137.48 002.58
VF1 001.09 002.27 002.40 000.25
WLD 462.51 464.64 464.99 000.43
IR 000.00 000.01 000.66 000.03
VF2 003.32 003.42 003.49 000.04


procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
s : string;
begin
ListBox1.Items.LoadFromFile('1111.txt');
// 刪除 Col Title
ListBox1.Items.Delete(0);
// 刪除 Row Title
for i := 0 to listbox1.count -1 do begin
s := ListBox1.Items[i];
delete(s,1,pos(#32,s));
// 將 空白 轉 成 ","
ListBox1.Items[i] := StringReplace(trim(s), #32, ',',[rfReplaceAll]);
end;
end;

------
努力會更接近成功
max5020
資深會員


發表:30
回覆:277
積分:321
註冊:2003-06-04

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-02-21 17:57:49 IP:59.125.xxx.xxx 訂閱
想要讀取文字檔內容,
又要分析文字檔格式內容,
好像沒有比較好的方法,
只能設一個TStrings,
再LoadFromFile,
然後for i:= 0 to Strings.Count-1 do
遂一滴,
分析每一行的文字內容,
我想只能這樣了!!,
ps. 我用這個方法,
找一個網頁,
分析網頁檔案內容,
固定讀取要的匯率數據,
做為匯率自動更新功能,
很白吃的做法吧!!!
編輯記錄
max5020 重新編輯於 2008-02-21 18:01:02, 註解 無‧
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-02-21 18:10:50 IP:122.127.xxx.xxx 訂閱
function GetRangeData(_Source,_Head,_Tail:string): string;
begin
ParsingRef(_Source,_Head); // 去頭
_Source := ParsingRef(_Source,_Tail); // 截尾
result := trim(_Source);
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.text:=GetRangeData('Bin01-01 Total QTY:2','Bin','-'); // 第一行
edit3.text:=GetRangeData('Bin01-01 Total QTY:2','QTY:',''); // 第二行
edit4.text:=GetRangeData('Date: 2007/12/27 09:04:02','Date:',''); // 第三行
: // 依此類推
:
end;

------
努力會更接近成功
編輯記錄
pceyes 重新編輯於 2008-02-21 18:11:56, 註解 隔行‧
st33chen
尊榮會員


發表:15
回覆:591
積分:1201
註冊:2005-09-30

發送簡訊給我
#7 引用回覆 回覆 發表時間:2008-02-22 13:54:56 IP:122.116.xxx.xxx 訂閱
我較常用字串來處理類似問題,
您的問題用兩種手法來處理可能比較好

第一段 (Min Avg Max Std 之前)

試者把整個檔案讀進來當成一個字串( ASTR)];
然後用 i:=POS('Bin', astr), i:=pos('Total QTY:', astr), i:=pos('LotNumber: ' , astr), ...,
i:=pos('Barcode No: ',astr), 等等, 就可找到所在位置, 然後用 copy(astr,i 12,1000) 存成 bstr,
再用 copy(bstr,1,pos(#13,bstr)-1) 取出值
這樣就不管出現的位置是否相同了,

第二段(Min Avg Max Std 之後 )
已有大大提出解法,

請參考, 謝謝

------
IS IT WHAT IT IS
我是 李慕白 請倒著唸.
又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦);
都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲.
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#8 引用回覆 回覆 發表時間:2008-02-22 15:37:55 IP:220.141.xxx.xxx 訂閱
interface
type
TForm1 = class(TForm)
Memo1: TMemo;
ListBox2: TListBox;
Button2: TButton;

var
Form1: TForm1;

{$R *.dfm}
// 抓區間用
function GetRangeData(_Source,_Head,_Tail:string): string;
begin
ParsingRef(_Source,_Head); // 去頭
_Source := ParsingRef(_Source,_Tail); // 截尾
result := trim(_Source);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Lines.LoadFromFile('file_1.txt');
end;

end.

Sample:
http://delphi.ktop.com.tw/download.php?download=upload/47be7d30f1d8a_split.zip

------
努力會更接近成功
編輯記錄
pceyes 重新編輯於 2008-02-22 15:44:48, 註解 sample Attatch‧
doom834
一般會員


發表:2
回覆:4
積分:1
註冊:2006-11-14

發送簡訊給我
#9 引用回覆 回覆 發表時間:2008-02-22 17:46:35 IP:59.125.xxx.xxx 訂閱

感謝pceyes的支援,我已完成程式,但是大大最新的程式碼,我會好好的參考,
以下是我的程式碼,寫的有點笨但是可以用,若有大大可以指導更精簡的程式,小弟會虛心學習,感謝!!!

[code delphi]
function StrSeg(Str ,SegSymbol: String ; SegIndex: integer): String;

var

r,c,s,sTmp: String;

i: integer;

begin

r := '';

if ((Str = '') or (SegIndex < 1)) then

begin

result:=r;

Exit;

end;

c := SegSymbol;

if (c = '') then c := ',';

i := 0;

s := Str;

sTmp := '';

while Pos(c,s) > 0 do

begin

i := i 1;

sTmp := Copy(s, 1, Pos(c, s) - 1);

if (SegIndex = i) then

begin

r:=sTmp;

Break;

end;

s := Copy(s, Pos(c, s) Length(c), Length(s) - (Pos(c, s) Length(c)) 1);

end;

if SegIndex = (i 1) then

begin

r := s;

end;

result := r;

end;


procedure TForm1.SB1Click(Sender: TObject);
var

s,str,ST,ED :String;
i,j,ST_1,ED_1 :Integer;
WLD1_S,WLP1_S,LOP1_S,VF1_S,IR1_S :Integer;
WLD2_S,WLP2_S,LOP2_S,VF2_S,IR2_S :Integer;

begin

SB3.Enabled:=False;
SB4.Enabled:=False;
ODL1.Filter:='sot files(Sorter轉出檔)|*.SOT'; //設定可開啟檔案類型
ASG1.Clear;
ASG1.RowCount:=2;
ASG1.ColCount:=46;
WLD1_S :=6;
WLP1_S :=10;
LOP1_S :=14;
VF1_S :=18;
IR1_S :=22;
WLD2_S :=26;
WLP2_S :=30;
LOP2_S :=34;
VF2_S :=38;
IR2_S :=42;

ASG1.Cells[0,0]:='BARCODE_NO';
ASG1.Cells[1,0]:='LOT_NUMBER';
ASG1.Cells[2,0]:='BIN_NO';
ASG1.Cells[3,0]:='QTY';
ASG1.Cells[4,0]:='FILE_DATE';
ASG1.Cells[5,0]:='M/C_NO';
ASG1.Cells[WLD1_S,0]:='WLD1_MIN';
ASG1.Cells[WLD1_S 1,0]:='WLD1_AVG';
ASG1.Cells[WLD1_S 2,0]:='WLD1_MAX';
ASG1.Cells[WLD1_S 3,0]:='WLD1_STD';
ASG1.Cells[WLP1_S,0]:='WLP1_MIN';
ASG1.Cells[WLP1_S 1,0]:='WLP1_AVG';
ASG1.Cells[WLP1_S 2,0]:='WLP1_MAX';
ASG1.Cells[WLP1_S 3,0]:='WLP1_STD';
ASG1.Cells[LOP1_S,0]:='LOP1_MIN';
ASG1.Cells[LOP1_S 1,0]:='LOP1_AVG';
ASG1.Cells[LOP1_S 2,0]:='LOP1_MAX';
ASG1.Cells[LOP1_S 3,0]:='LOP1_STD';
ASG1.Cells[VF1_S,0]:='VF1_MIN';
ASG1.Cells[VF1_S 1,0]:='VF1_AVG';
ASG1.Cells[VF1_S 2,0]:='VF1_MAX';
ASG1.Cells[VF1_S 3,0]:='VF1_STD';
ASG1.Cells[IR1_S,0]:='IR1_MIN';
ASG1.Cells[IR1_S 1,0]:='IR1_AVG';
ASG1.Cells[IR1_S 2,0]:='IR1_MAX';
ASG1.Cells[IR1_S 3,0]:='IR1_STD';
ASG1.Cells[WLD2_S,0]:='WLD2_MIN';
ASG1.Cells[WLD2_S 1,0]:='WLD2_AVG';
ASG1.Cells[WLD2_S 2,0]:='WLD2_MAX';
ASG1.Cells[WLD2_S 3,0]:='WLD2_STD';
ASG1.Cells[WLP2_S,0]:='WLP2_MIN';
ASG1.Cells[WLP2_S 1,0]:='WLP2_AVG';
ASG1.Cells[WLP2_S 2,0]:='WLP2_MAX';
ASG1.Cells[WLP2_S 3,0]:='WLP2_STD';
ASG1.Cells[LOP2_S,0]:='LOP2_MIN';
ASG1.Cells[LOP2_S 1,0]:='LOP2_AVG';
ASG1.Cells[LOP2_S 2,0]:='LOP2_MAX';
ASG1.Cells[LOP2_S 3,0]:='LOP2_STD';
ASG1.Cells[VF2_S,0]:='VF2_MIN';
ASG1.Cells[VF2_S 1,0]:='VF2_AVG';
ASG1.Cells[VF2_S 2,0]:='VF2_MAX';
ASG1.Cells[VF2_S 3,0]:='VF2_STD';
ASG1.Cells[IR2_S,0]:='IR2_MIN';
ASG1.Cells[IR2_S 1,0]:='IR2_AVG';
ASG1.Cells[IR2_S 2,0]:='IR2_MAX';
ASG1.Cells[IR2_S 3,0]:='IR2_STD';
ASG1.AutoSize:=True;
ASG1.FixedRows:=1;

if ODL1.Execute then

ED1.Text:= ODL1.FileName; //取得檔案名與完整路徑
ED1.Enabled:=False;

ListBox1.Items.LoadFromFile(ED1.Text);
for i := 0 to listbox1.count -1 do begin
s := ListBox1.Items[i];
// 將 空白 轉 成 ","
str :=copy(s,1,7);
if str = 'Barcode' then
begin

for j := i 2 to listbox1.count -1 do
begin
s := ListBox1.Items[j];
ListBox1.Items[j] := StringReplace(trim(s), #32, ',',[rfReplaceAll]);
ASG1.Cells[0,1]:=GetRangeData(ListBox1.Items[i],'Barcode No:','');
end;
end;
end;


ASG1.Cells[1,1]:=GetRangeData(ListBox1.Items[1],'LotNumber:','');
ASG1.Cells[2,1]:=GetRangeData(ListBox1.Items[0],'Bin','-');
ASG1.Cells[3,1]:=GetRangeData(ListBox1.Items[0],'QTY:','');
ASG1.Cells[4,1]:=GetRangeData(ListBox1.Items[2],'Date:','');
ASG1.Cells[5,1]:=GetRangeData(ListBox1.Items[3],'M/C No:','');

//尋找電性值開始位置
for i := 0 to listbox1.count -1 do begin
s := ListBox1.Items[i];
str := copy(s,1,3);
if str = 'Min' then
begin

ST := inttostr(i 2);
ST_1 := strtoint(ST);
break;
end;
end;

//尋找電性值結束位置
for i := ST_1 to listbox1.count -1 do begin
s := ListBox1.Items[i];
str := copy(s,1,3);
if str = '' then
begin

ED := inttostr(i);
ED_1 := strtoint(ED);
break;

end;
end;

for i := ST_1 to ED_1 do begin

str := StrSeg(ListBox1.Items[i-1],',',1);
if (str = 'WLD1') or (str = 'WLD') then begin
ASG1.Cells[WLD1_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[WLD1_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[WLD1_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[WLD1_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if (str = 'WLP1') or (str = 'WLP') then begin
ASG1.Cells[WLP1_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[WLP1_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[WLP1_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[WLP1_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if (str = 'LOP1') or (str = 'LOP') then begin
ASG1.Cells[LOP1_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[LOP1_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[LOP1_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[LOP1_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if (str = 'VF1') or (str = 'VF') then begin
ASG1.Cells[VF1_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[VF1_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[VF1_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[VF1_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if (str = 'IR1') or (str = 'IR') then begin
ASG1.Cells[IR1_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[IR1_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[IR1_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[IR1_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if str = 'WLD2' then begin
ASG1.Cells[WLD2_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[WLD2_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[WLD2_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[WLD2_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if str = 'WLP1' then begin
ASG1.Cells[WLP1_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[WLP1_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[WLP1_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[WLP1_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if str = 'LOP2' then begin
ASG1.Cells[LOP2_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[LOP2_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[LOP2_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[LOP2_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if str = 'VF2' then begin
ASG1.Cells[VF2_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[VF2_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[VF2_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[VF2_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;
if str = 'IR2' then begin
ASG1.Cells[IR2_S,1]:= StrSeg(ListBox1.Items[i-1],',',7);
ASG1.Cells[IR2_S 1,1]:= StrSeg(ListBox1.Items[i-1],',',9);
ASG1.Cells[IR2_S 2,1]:= StrSeg(ListBox1.Items[i-1],',',11);
ASG1.Cells[IR2_S 3,1]:= StrSeg(ListBox1.Items[i-1],',',13);
end;

end;

end;

end.

[/code]
系統時間:2024-05-17 6:47:49
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!