Find a specific value in a csv file
이전 댓글 표시
How can I make it?
In csv file, I want to find the year in Part 1 and D value ("D":"1") in Part2
Finally I want to make :
2022, 1
2021, 2
2020, 3
2019, 4
2018, 5
답변 (2개)
Maybe this will work on your file (if not, upload the file here)
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter',',','NumHeaderLines',1);
years = regexp(C(:,1),'(\d{4})','tokens','once');
years = vertcat(years{:})
d = regexp(C(:,end),'D:"(\d+)"','tokens','once');
d = vertcat(d{:})
% result as a cell array of character vectors:
result = [years d]
% result as a numeric matrix:
result = str2double([years d])
댓글 수: 9
Alexai
2022년 7월 16일
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter','\t','NumHeaderLines',1)
years = regexp(C(:,1),'/(\d{4})','tokens','once');
years = vertcat(years{:})
d = regexp(C(:,end),'"D":"(\d+)"','tokens','once');
d = vertcat(d{:})
% result as a cell array of character vectors:
result = [years d]
% or, result as a numeric matrix:
result = str2double([years d])
Voss
2022년 7월 16일
I'm not sure what you mean by "read next tab", because the file is read using tab as the delimiter:
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter','\t','NumHeaderLines',1)
so there are no tabs in C:
contains(C,sprintf('\t'))
However, if you want to get the part between the last slash and the end (which corresponds to where the tabs are in the file), of each character vector in the first column of C, you can do this:
% match a slash followed by any non-slash characters, occurring at the end
% (to prevent matching previous slash+stuff parts)
years = regexp(C(:,1),'/([^/]*)$','tokens','once');
years = vertcat(years{:})
And to make the regular expression for the "D" part more general (allow matching anything between the double-quotes, not just digits), you can do this:
% match the literal "D":" and return any characters after that, before the next '"'
d = regexp(C(:,2),'"D":"(.*?)"','tokens','once');
d = vertcat(d{:})
More about Regular Expressions: Operators and Characters in Regular Expressions
Another way to use regular expressions to achieve the same result would be to operate on the contents of the file directly, i.e., not on a cell array that comes from readcell.
Here's how that would work:
file_name = 'table_data.csv';
fid = fopen(file_name);
data = fread(fid,'*char').'; % read the file as a character vector
fclose(fid);
disp(data);
% match a slash, followed by any non-slash non-tab characters, followed by
% a tab, and return the stuff between the slash and the tab
% (note: not using 'once' this time because this regexp operates on the
% file's entire contents)
years = regexp(data,'/([^\t/]*)\t','tokens');
years = vertcat(years{:})
% match the literal '""D"":""', return the characters after that, before the next '""'
d = regexp(data,'""D"":""(.*?)""','tokens');
d = vertcat(d{:})
Alexai
2022년 7월 17일
Voss
2022년 7월 17일
Try to adapt some of the examples I've given. Refer to the link I shared.
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter','\t','NumHeaderLines',1)
years = regexp(C(:,1),'/([^/]*/[^/]*)$','tokens','once');
years = vertcat(years{:})
Hi!
Try this:
% Create, split, and extract from part 1
part1 = "A/1/2/" + string(2022:-1:2018) ;
part1 = split(part1, '/') ;
yrs = part1(:,:,end) ;
% Create, split, and extract from part 2
part2 = " ""B"":""1"", ""C"":""2"", ""D"":""" + string(1:5) + '"' ;
part2 = split(part2, '"') ;
dig = str2double(part2(:,:,end-1)) ;
% Result
result = transpose(yrs + "," + dig )
Use datetime if you want to convert this string array to date time array.
Please keep in mind this a way from many to solve it, you can also use regular experssion or patterns to do this.
카테고리
도움말 센터 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!