Is there a less bug-prone way to include quote in selector string variable when defining opt.TableSelector?
3 ビュー (過去 30 日間)
古いコメントを表示
My situation is to readtable( ) several tables, each of which has a specific title, from an html file. I have come up with a couple solutions to define TableSelector, but it is bug prone. Is there a better way?
opt = htmlImportOptions;
opt.TableSelector = "//TABLE[contains(.,'Cash dividends')]";
headings = ["'Music'", "Photo"];
%% correct selector string, but bug prone
opt.TableSelector = "//TABLE[contains(.," + headings(1) +")]";
opt.TableSelector
% readtable(html, opt)
%% wrong solution
opt.TableSelector = "TABLE[contains(.,"+ headings(2) +")]";
opt.TableSelector
% readtable(html, opt) % would return no error message, so it's a bug source.
%% fix the wrong solution
str1 = "TABLE[contains(.,'";
str2 = "')]";
opt.TableSelector = str1 + headings(2) + str2;
opt.TableSelector
% readtable(html, opt)
3 件のコメント
Walter Roberson
2023 年 5 月 15 日
Reading further, I see that we do not need to worry about "escape characters" -- for example if the user specified 'Music\' then the \' would be treated as two distinct unrelated characters, not as indicating "the next character is a literal quote". So we do not need to "sanitize" the input against an assortment of characters.
We do, however, potentially need to worry about the possibility that the headings contain ' characters or " characters.
採用された回答
Walter Roberson
2023 年 5 月 17 日
opt.TableSelector = "TABLE[contains(.,'" + headings(2) + "')]";
or
opt.TableSelector = compose("Table[contains(.,'%s')", headings(2));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Time Series Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!