Extracting data from a .txt file
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ok i have created a theoretical situation for what i need. I have copious data which i can output in a .txt file that i need to parse and extract only a few useful elements. I have attached a sample output file.
What i need:
-I only care about animal hospitals so i need to go through and find out what animal hospitals are displayed
-I need to know how many cats and dogs are in each animal hospital, not a sum, just an individual listing. Animal Hospital X has Y dogs.
-I do not care about pandas only cats and dogs.
-Any help/advice is greatly appreciated
0 Kommentare
Antworten (2)
dpb
am 20 Jun. 2016
Bearbeitet: dpb
am 20 Jun. 2016
>> c=textread('data.txt','%s','delimiter','\n','whitespace','','headerlines',2);
>> ix=~cellfun(@isempty,strfind(c,'Animal Hospital')) | ...
~cellfun(@isempty,strfind(c,'Dogs')) | ...
~cellfun(@isempty,strfind(c,'dogs'));
>> c(ix)
ans =
'Animal Hospital Pennsylvania:'
'dogs:7 '
'Animal Hospital New York:'
'dogs:8'
'Animal Hospital California:'
'Dogs: 44'
>>
If the real file is normalized so there's not a capitalization issue, can shorten the second search for both forms of 'dogs|Dogs' to the proper case....
0 Kommentare
Shameer Parmar
am 21 Jun. 2016
clc;
clear all;
Data = textread('Data.txt', '%s', 'delimiter', '');
count = 1;
for i = 1: length(Data)
if ~isempty(strfind(Data{i},'Hospital')) || ~isempty(strfind(Data{i},'hospital')) ...
|| ~isempty(strfind(Data{i},'Cat')) || ~isempty(strfind(Data{i},'cat'))...
|| ~isempty(strfind(Data{i},'Dog')) || ~isempty(strfind(Data{i},'dog'))
newData{count} = strtrim(strrep(Data{i},':',' '));
count = count + 1;
end
end
newData = newData';
count = 1;
for i = 1:length(newData)
if ~isempty(strfind(newData{i},'Animal'))
newStr{count,:} = [newData{i},' has'];
for j = i+1:length(newData)
if isempty(strfind(newData{j},'Animal'))
newStr{count,:} = [newStr{count,:},' ',newData{j},' and'];
else
newStr{count,:} = [newStr{count,:}(1,1:(length(newStr{count,:})-4)),'.'];
newStr{count,:} = strrep(newStr{count,:},' ','');
break;
end
end
count = count + 1;
end
end
newStr{end,:} = [newStr{end,:}(1,1:(length(newStr{end,:})-4)),'.'];
newStr{end,:} = strrep(newStr{end,:},' ',' ');
newStr
Output will be:
newStr =
'Animal Hospital Pennsylvania has dogs 7 and cats 8.'
'Animal Hospital New York has dogs 8 and cats 7.'
'Animal Hospital California has Dogs 44.'
0 Kommentare
Siehe auch
Kategorien
Mehr zu Animation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!