Read CSV into table, but quoted text data contain new-lines
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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:
"...some text</p><p>
</p><p>
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
have 20.
Note: readtable detected the following parameters:
'Delimiter', ',', 'HeaderLines', 0, 'ReadVariableNames', true, 'Format',
'%f%f%f%q%q%q%q%q%q%f%f%f%f%q%q%f%D%q%f%f%q'
Line 3 is the "</p><p>" in the sample above.
0 commentaires
Réponse acceptée
Walter Roberson
le 12 Juil 2021
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');
fclose(fid)
dbtype(tname)
fid = fopen(tname, 'r');
data_cell = textscan(fid, '%q%q', 'delimiter', ',')
fclose(fid)
data_cell{1}
data_cell{2}
It turned out to be important to not use comma between the two %q and to use 'delimiter', ','
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Import and Export dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!