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

更改.txt文件中的文本并在MATLAB中创建新的文件输出,matlab,if-statement,while-loop,low-level-io,Matlab,If Statement,While Loop,Low Level Io,如果标题看起来有点不对劲,我提前道歉。我很难决定到底该给它取什么名字。无论如何,我现在做的基本上都是关于低级I/O的家庭作业。在我的一个作业中,我给出了两个.txt文件,一个包含电子邮件地址列表,另一个包含不再在电子邮件列表中的成员列表。我要做的是从第二个列表中删除成员的电子邮件。此外,在.txt文件中可能会有一些令人不快的惊喜。我必须清理电子邮件,并删除电子邮件后任何不需要的标点符号,如分号、逗号和空格。此外,我需要将所有文本小写。我在不止一个方面努力解决这个问题(我不完全确定如何让我的文件在

如果标题看起来有点不对劲,我提前道歉。我很难决定到底该给它取什么名字。无论如何,我现在做的基本上都是关于低级I/O的家庭作业。在我的一个作业中,我给出了两个.txt文件,一个包含电子邮件地址列表,另一个包含不再在电子邮件列表中的成员列表。我要做的是从第二个列表中删除成员的电子邮件。此外,在.txt文件中可能会有一些令人不快的惊喜。我必须清理电子邮件,并删除电子邮件后任何不需要的标点符号,如分号、逗号和空格。此外,我需要将所有文本小写。我在不止一个方面努力解决这个问题(我不完全确定如何让我的文件在输出中写入我需要的内容),但现在我主要关心的是以正确的顺序输出取消订阅消息。索特罗似乎不管用

以下是一些测试用例:

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
喜欢nope的人:

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

下面是我对这个问题的实现。代码在每一步都有注释,应该很容易理解。我尽可能地使用正则表达式,因为这是他们擅长的事情。。。还要注意,代码中没有任何循环:)

退订
给定以下文本文件:

list.txt names.txt 以下是我在运行代码时得到的结果:

>> unsubscribe('list.txt', 'names.txt')
list_messages.txt list_updated.txt
您是否有每个电子邮件地址对应的姓名列表?您的解决方案通过每个人的名字来称呼他们,但您提供的示例输入不包括他们。@rayryeng我知道我忘了什么。加进去了对不起!还是有点不清楚。文本文件的格式是什么?名称是否先出现,然后是电子邮件地址?您的示例输入仍然令人困惑。有一个电子邮件列表,然后是一个单独的电子邮件地址列表。这看起来像是一大堆我不知道如何使用的东西Regex'就是其中之一。我不知道“\w+”是什么东西。我也从未使用过文件部件。我们只被教导像fprintf或只是打开和写入它。不管怎样,我测试了它,看看是否可以使用它的结构,并将它的一部分更改为我知道的操作,但它在第12行“emails_exclude=strcat(cellfun(@(str)str(1),first_last(:,1)),first_last(:,2),”中出错3@gatech.edu“);”它说超出了矩阵的维数。顺便说一句,我很感谢你的帮助。@JessicaMarie:
regexp
或正则表达式是你真正想学的东西。它们在处理半自由形式的文本时非常有用。网上有一个不错的教程,你可以试试:。您突出显示的这一行根据名字/姓氏构建电子邮件地址:我从名字中提取第一个字母,然后附加姓氏和域部分
strcat
可以处理单元格数组和普通字符的混合输入。我建议您在调试器中运行代码并逐行检查,同时在每一步检查变量。
fileparts
是一个用于分隔完整文件名的路径/名称/扩展名的函数。因此,给定一个类似于
C:\path\to\file.txt
的输入,它将把它分成
C:\path\to
作为路径,
file
作为名称,
.txt
作为扩展名。我使用此函数获取原始邮件列表文件名,并将更新后的部分添加到其中。+1-永远爱你的帖子Amro:)。另外,感谢您链接到
regexone
。是时候复习一下我的
regex
,因为这里的许多问题都可以用它雄辩地解决了。@Amro我正在调试它,并试图用我所知道的修改它,但我突出显示的那行,电子邮件排除行,出错了。不知怎的,你编制的索引超出了范围,但我不完全确定你是怎么做的,主要是因为我不懂格式。
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
>> unsubscribe('list.txt', 'names.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.
ttoadstool3@gatech.edu
ydinosaur3@gatech.edu
wbadguy3@gatech.edu
fkong3@gatech.edu
dbones3@gatech.edu