我有一个csv
文件,其中包含以下行:
"some text", "more text hello 392 392", "etc complicated string here with spaces and commas"
我怎样才能把这个文件读成一个大矩阵?
因此,您要求简单的文本阅读?
fid=fopen('dummy.csv');
a=[];
while(1) ;
tline = fgetl(fid);
if ~ischar(tline)
break;
end
a=[a;char(tline)];
end
fclose(fid);
在我看来,最明智的方法是使用正则表达式来匹配正确的模式(引号和逗号对):
%// Read lines as strings
fid = fopen('input.txt', 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
%// Tokenize each line using a regular expression
C = cellfun(@(x){[x{:}]}, regexp(C{:}, '"([^"]*)"(?:\s*,\s*)?', 'tokens'));
生成的元胞数组C
应包含所有所需的逗号分隔值作为标记。
假设您的文件名为“input.csv”,并包含以下内容:
"some text", "more text hello 392 392", "string spaces and commas ,,,"
"hello, world!"
后:
fid = fopen('input.csv', 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
C = cellfun(@(x){[x{:}]}, regexp(C{:}, '"([^"]*)"(?:\s*,\s*)?', 'tokens'));
结果应该是:
C(1, 1) =
'some text'
'more text hello 392 392'
'string spaces and commas ,,,'
C(1, 2) =
'hello, world!'