讀取文本文件,查找包含特定字符串的行大家好,
這是我有一個有效的腳本。 我想知道是否有更好的方法來做同樣的事情。
我首先使用“加載文本文件”將文本文件讀入字符串變量“file_text1”。
接下來,我使用“words”和“\!n”來分隔行並將它們加載到字符串數組“file_text2”中。
接下來,我將“file_text2”加載到數據表中。 然後,我使用“獲取行所在位置”與“包含”相結合來查找原始文本文件中包含特定字符串的行號。
是否有一個函數相當於“在哪裡獲取行”但可用於字符串數組? 這樣,我可以跳過加載到數據表中的部分。
file_text1 = load text file( "example.txt" );
file_text2 = words(file_text1 , "\!n");
dt0 = new table("file_dt", new column("file_content", character) );
dt0:file_content << set values(file_text2);
pass_qty_row_array = dt0 << get rows where(contains( :file_content, "Pass") );高級統計建模自動化和腳本編寫消費者和市場研究內容組織數據混合和清理大規模定制質量和工藝工程回复:讀取文本文件,查找包含特定字符串的行謝謝大家,特別是 jthi。
我已經確定對 N 個項目(file_text2)使用 for 循環比使用數據表更快。 然後我看到 jthi 的方法使用“For Each”,從時間測量來看它甚至更快。
回复:讀取文本文件,查找包含特定字符串的行還有 For Each、Filter Each(和 Transform Each)可以幫助解決此類情況,而且速度應該相當快。
Names Default To Here(1);
file_txt = Load Text File("$SAMPLE_IMPORT_DATA/Animals.txt");
file_list = words(file_txt, "\!n");
search_word = "fall";
pass_qty_row_array = [];
For Each({line, idx}, file_text2,
If(Contains(line, search_word),
Insert Into(pass_qty_row_array, idx);
);
);
show(pass_qty_row_array);
回复:讀取文本文件,查找包含特定字符串的行就我個人而言,我為此類問題創建了 Map()、Filter() 和 Reduce() 函數。下面是一個使用 Map() 函數的示例,在我的系統上,它比創建表進行搜索大約快 50% - 60%。
這是映射函數(顯式全球化):
::map = Function( {inputs /* list, function */ },
/* uses a single underscore _ as the wild-card */
{__i__, __result__, __list__, _, __, __keys__},
__list__ = Eval( Arg( inputs, 1 ) );
If( Is List( __list__ ),
__result__ = {};
Eval(
Substitute(
Expr(
For( __i__ = 1, __i__ <= __N__, __i__++,
_ = __list__[__i__];
__result__[__i__] = __function__
)
)
,
Expr( __N__ ), N Items( __list__ ),
Expr( __function__ ), Arg( inputs, 2 )
);
);
,
Is Associative Array( __list__ ),
__result__ = [=>];
__keys__ = __list__ << Get Keys;
Eval(
Substitute(
Expr(
For( __i__ = 1, __i__ <= __N__, __i__++,
__ = __keys__[__i__];
_ = __list__[__];
__result__[__] = __function__
)
)
,
Expr( __N__ ), N Items( __keys__ ),
Expr( __function__ ), Arg( inputs, 2 )
)
)
);
__result__
);以下是使用表與映射函數的比較:
Names Default To Here( 1 );
filename = Convert File Path( "$SAMPLE_IMPORT_DATA/UN Malaria 2009.csv", absolute, windows );
result = Load Text File( filename );
Show( result );
file_text2 = Words( result, "\!N" );
N = 10000;
word = "malaria";
s = HP Time();
Summation( i = 1, N,
dt0 = New Table( "file_dt", New Column( "file_content", character ), Private );
dt0:file_content << set values( file_text2 );
pass_qty_row_array 1 = dt0 << get rows where( Contains( :file_content, word ) );
close( dt0, No Save );
0
);
Show( time 1 = (HP Time() - s) / 1000000 );
s = HP Time();
Summation( i = 1, N,
pass_qty_row_array 2 = loc( Matrix( ::map({ file_text2, Contains( _, word ) }) ) );
0
);
Show( time 2 = (HP Time() - s) / 1000000 );<BR /><BR />Show( All( pass_qty_row_array 1 == pass_qty_row_array 2 ) );
1 - (time 1 - time 2) / time 1除了定義映射函數之外,使用它通常比任何其他解決方案在代碼中看起來更清晰。
回复:讀取文本文件,查找包含特定字符串的行這是處理這個問題的另一種方法。可能會更快,也可能會更慢……我不知道。
names default to here(1);
dt0 = Open(
"example.txt",
columns( New Column( "Line", Character, "Nominal" ) ),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( CSV( 0 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
pass_qty_row_array = dt0 << get rows where(contains( :file_content, "Pass") );