Extracting data from a .txt file

조회 수: 1 (최근 30일)
John  Bowling
John Bowling 2016년 6월 20일
답변: Shameer Parmar 2016년 6월 21일
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

답변 (2개)

dpb
dpb 2016년 6월 20일
편집: dpb 2016년 6월 20일
>> 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....

Shameer Parmar
Shameer Parmar 2016년 6월 21일
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.'

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by