2013-04-10 95 views
2

我在解析文本文件中的CSV時遇到問題,並想知道您是否可以幫助我。到目前爲止,我有以下內容:REXX - 從CSV文件解析

CSV文件(DATA.txt)看起來像這樣,它總是會有15個字段全部用逗號分隔。並非所有字段都是強制性的,所以有些字段會被填充,有些字段是空白的

Seattle,Lastname,Firstname,DOB,SEX,etc,etc 
Seattle,Lastname,Firstname,DOB,,etc,etc 
Portland,Lastname,Firstname,DOB,SEX,,,etc 
Portland,Lastname,Firstname,DOB,SEX,etc,etc 

這裏是我的REXX代碼

SOURCEFILE = "C:\DATA\DATA.TXT" 
IF A=2 THEN DO COUNTER=1 TO LINES(SOURCEFILE) 
    PARSE VALUE LINEIN(SOURCEFILE) WITH CITY "," LAST_NAME "," FIRST_NAME "," MOM_NAME "," MIDDLE_NAME "," DAD_NAME "," DOB "," etc "," etc "," etc "," etc "," SEX "," etc "," etc 
    CALL SETCURSOR 4,23 
    CALL CREATEDATA 
END 

CREATEDATA: 
CALL TYPE CITY 
CALL PRESS TAB 
CALL TYPE LAST_NAME 
CALL PRESS TAB 
CALL TYPE DATE(U) 
CALL PRESS TAB 
CALL TYPE FIRST_NAME 
CALL PRESS TAB 
CALL PRESS ENTER 
RETURN 

我不知道我是否應該在解析時使用ARG或VAR或者,如果我寫了正確的前兩行。事實上,我知道我的CREATEDATA函數正常工作,因爲我得到了輸入的「CITY」而不是解析值。任何幫助將非常感激。謝謝!

回答

1

幾點意見:

1)Lines(SourceFile) Windows系統可能包括讀取整個文件上計算CR-LF序列。然後你的Parse value LineIn(SourceFile)循環再次讀取它。典型的Rexx的方式做,這將是:

Address SYSTEM 'TYPE' SourceFile with output stem Lines. 
Do Counter = 1 to Lines.0 
    Parse var Lines.Counter ... 
End 
Drop Lines. 

至少,只要該文件不是太大,保持它在陣列中存儲成本。

2)您在循環結束時流入CreateData,這就是您看到「CITY」的原因。在End之後,您需要一個ReturnExit指令。

3)根據#2,顯然Parse從未被執行,因爲City未初始化(Rexx中的未初始化變量的值是大寫的名稱)。它的條件是A=2,但情況並非如此。

+0

根據Cowlishaw的Rexx的書,建在函數中的行可能返回的行數中引用的文件或,如果不能確定,「1」,其中一個非零計數將適當,否則爲'0'。我在窗口上使用ooRexx相當多,可以確認ooRexx不計算所有行,它只返回0/1。我使用以下命令一次讀取一行文件:DO WHILE LINES(filename)> 0; PARSE VALUE LINEIN(文件名)與...; END – NealB 2013-04-12 13:38:18

+0

「Lines()」的結果與實現有關。有些實現會返回一個計數,其他實例只會返回1或0。這也是爲什麼我更喜歡加載詞幹:'Lines.0'是一個實際的計數。 – 2013-04-12 23:33:01

1

一個問題是什麼,如果A = 2,那麼在

IF A=2 THEN DO COUNTER=1 TO LINES(SOURCEFILE) 

如果A宗旨!= 2的循環被旁路。我懷疑你的程序應該是:

SOURCEFILE = "C:\DATA\DATA.TXT" 
DO COUNTER=1 TO LINES(SOURCEFILE) 
    PARSE VALUE LINEIN(SOURCEFILE) WITH CITY "," LAST_NAME "," FIRST_NAME "," MOM_NAME "," MIDDLE_NAME "," DAD_NAME "," DOB "," etc "," etc "," etc "," etc "," SEX "," etc "," etc 
    CALL SETCURSOR 4,23 
    CALL CREATEDATA 
END 

RETURN /* prevent the fall through to createdata */ 

CREATEDATA: 




--------------------------- 

解析的語句具有以下基本格式

解析[來源] [解析控制]

其中[來源] icludes

ARG - 過程調用參數 拉 - 數據從堆棧中取出 var - 數據來自變量 值...提供數據在線

所以你的解析也能像

linein = LINEIN(SOURCEFILE) 
    PARSE var linein CITY "," LAST_NAME "," FIRST_NAME "," MOM_NAME "," MIDDLE_NAME "," DAD_NAME "," DOB "," etc "," etc "," etc "," etc "," SEX "," etc "," etc 

DO COUNTER=1 TO LINES(SOURCEFILE) 
     CALL SETCURSOR 4,23 
     CALL CREATEDATA LINEIN(SOURCEFILE) 
    END 

    RETURN /* prevent the fall through to createdata */ 

    CREATEDATA: 
    parse arg CITY "," LAST_NAME "," FIRST_NAME "," MOM_NAME "," MIDDLE_NAME "," DAD_NAME "," DOB "," etc "," etc "," etc "," etc "," SEX "," etc "," etc 

最後屁股羅斯說你應該嘗試aviod線(的資源文件),因爲它涉及到整個文件讀