更改.txt文件中的文本并在MATLAB中创建新文件输出

时间:2014-10-14 02:36:44

标签: matlab if-statement while-loop low-level-io

如果标题看起来有些偏差,我会提前道歉。我无法确定我应该给它命名的内容。无论如何,基本上我现在正在做的是完全功课,处理低级I / O.对于我的一项任务,我提供了两个.txt文件,一个包含电子邮件地址列表,另一个包含不再列在电子邮件列表中的列表成员。我要做的是从第二个列表中删除成员的电子邮件。此外,.txt文件中可能存在一些令人讨厌的意外。我必须清理电子邮件并在电子邮件后删除任何不需要的标点符号,例如分号,逗号和空格。此外,我需要小写所有文本。我以多种方式解决这个问题(我不完全确定如何让我的文件在我的输出中写出我需要的东西),但是现在我的主要关注点是输出取消订阅消息按正确顺序排列。 Sortrow似乎不起作用。

以下是一些测试用例:

Test Cases
unsubscribe('Grand Prix Mailing List.txt', ...
              'Unsubscribe from Grand Prix.txt')
     => output file named 'Grand Prix Mailing List_updated.txt' that looks
        like 'Grand Prix Mailing List_updated_soln.txt'
     => output file named 'Unsubscribe from Grand Prix_messages.txt' that 
        looks like 'Unsubscribe from Grand Prix_messages_soln.txt'

原始邮件列表

Grand Prix Mailing List:
MPLUMBER3@gatech.edu, 
lplumber3@gatech.edu 
Ttoadstool3@gatech.edu;
bkoopa3@gatech.edu
ppeach3@gatech.edu,
ydinosaur3@gatech.edu
kBOO3@gatech.edu
WBadguy3@gatech.edu;
FKong3@gatech.edu
dkong3@gatech.edu
dbones3@gatech.edu

像黑人一样的人:

MARIO PLUMBER; 
bowser koopa 
Luigi Plumber,
Donkey Kong 
King BOO;
Princess Peach

之后看起来应该是什么样的:

ttoadstool3@gatech.edu
ydinosaur3@gatech.edu
wbadguy3@gatech.edu
fkong3@gatech.edu
dbones3@gatech.edu

我的文件输出:

Mario, you have been unsubscribed from the Grand Prix mailing list.
Luigi, you have been unsubscribed from the Grand Prix mailing list.
Bowser, you have been unsubscribed from the Grand Prix mailing list.
Princess, you have been unsubscribed from the Grand Prix mailing list.
King, you have been unsubscribed from the Grand Prix mailing list.
Donkey, you have been unsubscribed from the Grand Prix mailing list.

所以Amro一直很友善地提供解决方案,虽然它比我现在所知的要高一点。我现在的主要问题是,当我输出取消订阅消息时,我需要它与原始电子邮件列表的顺序相同。例如,当Bowser在Luigi之前的投诉名单上时,在取消订阅的消息中,Luigi需要来到他面前。

这是我的原始代码:

function[] = unsubscribe(email_ids, member_emails)
    Old_list = fopen(email_ids, 'r'); %// opens my email list
    Old_Members = fopen(member_emails, 'r'); %// Opens up the names of people who want to unsubscribe
    emails = fgets(Old_list); %// Reads first line of emails
    member_emails = [member_emails]; %// Creates an array to populate
while ischar(emails) %// Starts my while loop
%// Pulls out a line in the email
    emails = fgets(Old_list);
%// Quits when it sees this jerk
    if emails == -1
        break;
    end

%// I go in to clean stuff up here, but it doesn't do any of it. It's still in the while loop though, so I am not sure where the error is
proper_emails = lower(member_emails); %// This is supposed to lowercase the emails, but it's not working
unwanted = findstr(member_emails, ' ,;');
member_emails(unwanted) = '';
member_emails = [member_emails, emails];
end

while ischar(Old_Members) %// Does the same for the members who want to unsubscribe
    names = fgetl(member_emails);
    if emails == -1
        break
    end
proper_emails = lower(names); %// Lowercases everything
unwanted = findstr(names, ' ,;');
names(unwanted) = '';
end

Complainers = find(emails);

New_List = fopen('Test2', 'w'); %// Creates a file to be written to
fprintf(New_List, '%s', member_emails); %// Writes to it
Sorry_Message = fopen('Test.txt', 'w');
fprintf(Sorry_Message, '%s', Complainers);

%// Had an issue with these, so I commented them out temporarily
%// fclose(New_List);
%// fclose(Sorry_Message);
%// fclose(email_ids); 
%// fclose(members);

end

1 个答案:

答案 0 :(得分:3)

以下是我对该问题的实施。代码在每一步都被注释,应该易于理解。我正在使用正则表达式,因为这是他们擅长的东西......还要注意我在代码中没有任何循环:)

unsubscribe.m

function unsubscribe(mailinglist_file, names_file)

    %%
    % read list of names of those who want to unsubscribe
    names = read_file(names_file);

    % break names into first/last parts
    first_last = regexp(names, '(\w+)\s+(\w+)', 'tokens', 'once');
    first_last = vertcat(first_last{:});

    % build email handles (combination of initials + name + domain)
    emails_exclude = strcat(cellfun(@(str) str(1), first_last(:,1)), ...
        first_last(:,2), '3@gatech.edu');

    %%
    % read emails in mailing list
    emails = read_file(mailinglist_file);

    % update emails by removing those who wish to unsubscribe
    emails(ismember(emails, emails_exclude)) = [];

    %%
    % write updated mailing list
    [~,fName,fExt] = fileparts(mailinglist_file);
    fid = fopen([fName '_updated' fExt], 'wt');
    fprintf(fid, '%s\n', emails{:});
    fclose(fid);

    % write list of names removed
    % capilaize first letter of first name
    first_names = cellfun(@(str) [upper(str(1)) str(2:end)], ...
        first_last(:,1), 'UniformOutput',false);
    msg = strcat(first_names, ...
        ', you have been unsubscribed from the mailing list.');
    fid = fopen([fName '_messages' fExt], 'wt');
    fprintf(fid, '%s\n', msg{:});
    fclose(fid);

end

function C = read_file(filename)
    % read lines from file into a cell-array of strings
    fid = fopen(filename, 'rt');
    C = textscan(fid, '%s', 'Delimiter','');
    fclose(fid);

    % clean up lines by removing trailing punctuation
    C = lower(regexprep(C{1}, '[,;\s]+$', ''));
end

给出以下文本文件:

LIST.TXT

MPLUMBER3@gatech.edu, 
lplumber3@gatech.edu 
Ttoadstool3@gatech.edu;
bkoopa3@gatech.edu
ppeach3@gatech.edu,
ydinosaur3@gatech.edu
kBOO3@gatech.edu
WBadguy3@gatech.edu;
FKong3@gatech.edu
dkong3@gatech.edu
dbones3@gatech.edu

names.txt中

MARIO PLUMBER; 
bowser koopa 
Luigi Plumber,
Donkey Kong 
King BOO;
Princess Peach

这是我在运行代码时得到的结果:

>> unsubscribe('list.txt', 'names.txt')

list_messages.txt

Mario, you have been unsubscribed from the mailing list.
Bowser, you have been unsubscribed from the mailing list.
Luigi, you have been unsubscribed from the mailing list.
Donkey, you have been unsubscribed from the mailing list.
King, you have been unsubscribed from the mailing list.
Princess, you have been unsubscribed from the mailing list.

list_updated.txt

ttoadstool3@gatech.edu
ydinosaur3@gatech.edu
wbadguy3@gatech.edu
fkong3@gatech.edu
dbones3@gatech.edu