Read CSV into table, but quoted text data contain new-lines
조회 수: 17(최근 30일)
I am using "readtable" to read a CSV file into a table. The first row consist of column headings, and there are no row names. Some quoted text data contain new-lines, such as in this single string consisting of multiple lines:
more text...end text. "
The "readtable" function interprets the 2nd line above as a new record. None of the "readtable" parameters seem to be for recognizing newlines within a quote string as part of the sting itself. Is it possible to do this with "readtable"?
According to the following thread, it should be enough that the multi-line string be enclosed in quotes:
However, I get the error:
Error using readtable (line 216)
Reading failed at line 3. All lines of a text
file must have the same number of delimiters.
Line 3 has 0 delimiters, while preceding lines
Note: readtable detected the following parameters:
'Delimiter', ',', 'HeaderLines', 0, 'ReadVariableNames', true, 'Format',
Line 3 is the "</p><p>" in the sample above.
Walter Roberson 2021년 7월 12일
In order to have any possibility of success using readtable() you would need to use detectImportOptions and then set the LineEnding option to be empty. However, you would then have problems at end of line.
I suggest that you use textscan() instead.
tname = tempname()
fid = fopen(tname, 'w');
cleanMe = onCleanup(@() delete(tname));
fprintf(fid, '"...some text</p><p>\n</p><p>\nmore text...end text. ", "hello"\n"line 2", "L2 field 2"\n');
fid = fopen(tname, 'r');
data_cell = textscan(fid, '%q%q', 'delimiter', ',')
It turned out to be important to not use comma between the two %q and to use 'delimiter', ','