如何读取csv文件中的单独字段并与用户输入进行比较? [英] How to read seperate fields in a csv file and compare with user input?

查看:225
本文介绍了如何读取csv文件中的单独字段并与用户输入进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个应用程式,使用者可以在3个分隔栏中上传包含名字,姓氏和电子邮件的CSV档案。用户界面将有3个文本字段和一个按钮。当用户输入名字或姓氏或电子邮件,并单击搜索按钮时,必须搜索整个文档,并显示一条警告,说明在文件中找到了记录。这是我使用的函数,但它只读取第一行和第一列。请帮助

I am building an app where use can upload a csv file that contains First Name, Last Name and email in 3 seperate columns. User interface will have 3 text fields and a button. When user enters the first name or last name or email, and click search button, the whole documenent must be searched and display an alert saying that the record was found in the file. This is the function that I am using, but it only reads the first row and first column. Please help

- (void) SearchStudent

{

    NSArray *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);



    NSString *DocumentDirectory = [DocumentPath objectAtIndex:0];



    NSString *FullPath = [DocumentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"example.csv"]];



    NSString * pstrCSVFile= [NSString stringWithContentsOfFile:FullPath encoding:NSASCIIStringEncoding error:NULL];



    NSArray * paRowsOfCSVFile= [pstrCSVFile componentsSeparatedByString:@"\n"];

    NSArray *paColumnsOfRow;

    NSString *pstrFirstColumn;



    for(NSString * pstrRow in paRowsOfCSVFile)

    {

        paColumnsOfRow= [pstrRow componentsSeparatedByString:@","];

        pstrFirstColumn= [paColumnsOfRow objectAtIndex:0];

        if([pstrFirstColumn localizedCaseInsensitiveCompare:GWIDText.text] == NSOrderedSame)

        {

            UIAlertView *alertingFileName = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

            [alertingFileName show];

            break;

        }

        else

        {

            UIAlertView *alertingFileName1 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Not Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

            [alertingFileName1 show];

            break;

        }

    }


}


推荐答案

几个提示:


  1. You are checking for one column only.

if([pstrFirstColumn localizedCaseInsensitiveCompare:GWIDText.text] == NSOrderedSame)


对其他两列执行相同操作。然后你将解决你的第一个只检查一列的问题。

Do the same with the other two columns. Then you will solve your first problem of only checking one column.


  1. 看起来行终止符不正确。根据创建csv文件的平台,它可能有不同的行终止符。我建议采取一个文本编辑器像notepad ++例如,查看隐藏的代码,以找出你的行终止符。然后使用终结符拆分行。确保您获得的行数超过1行(只输出行变量)。

这篇关于如何读取csv文件中的单独字段并与用户输入进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆