Using readtable with double-quote strings in .csv file

3 ビュー (過去 30 日間)
a.tom
a.tom 2015 年 2 月 17 日
回答済み: Al in St. Louis 2022 年 5 月 19 日
I am trying to read in a .csv file with double-quoted strings (i.e. "asdf") separated by commas. It does not seem that readtable can handle this gracefully when I have a string that contains a comma (i.e. "asdf,asdf"). It sees this as additional columns or something. The .csv file works fine with Excel 2007.
A sample/example file 'runFile.csv' with 3 columns. The top-row is:
"runName","runCount","runVal",
"runName" is an arbitrary string that may contain commas. "runCount", "resultVal" are doubles/integers.
Full sample file:
"runName","runCount","resultVal",
"asdf,asdf,asdf",1,1234,
"asdf,asdf,asdf",2,2345,
"asdf,asdf,asdf",3,3465,
When I run this in R2014b-64b (under Windows 7, 64-bit), I get:
>> T = readtable('runFile.csv')
Error using readtable (line 129)
Cannot interpret data in the file "runFile.csv'. Found 4 variable names but 6 data columns. You may need to specify a different format string, delimiter, or number of header lines.
I tried to use the extra parameter 'Format' with '%s%f%f' or something, but could not get it to work. I tried variations like '"%s"%f%f or something to no success.
I see that the release notes for R2014b shows some note about writetable now supporting double-quote strings using the 'QuoteStrings' parameter, but I could not find much more about this, or whether it is supported in readtable.
Thanks, Alex
  1 件のコメント
Al in St. Louis
Al in St. Louis 2022 年 5 月 19 日
This is still a problem with R2021a

サインインしてコメントする。

回答 (2 件)

Kuang-Chieh Yen
Kuang-Chieh Yen 2016 年 9 月 10 日
編集済み: Kuang-Chieh Yen 2016 年 9 月 10 日
The data format, %q, can be used to read data with double-quotes. So, you should replace %f and by %q. Maybe it works well. Moreover, I also recommend to run "datastore", for example
% get the information of yourdata
ds = datastore('yourdata.csv');
% import yourdata into T
T = read(ds);
HOPE IT USEFUL !

Al in St. Louis
Al in St. Louis 2022 年 5 月 19 日
Switch from MATLAB to Python. Pandas has no problem with a properly constructed CSV file. It's sad that readtable can't parse something this simple.

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by