解析.csv文件中的数据 [英] parsing data from .csv file

查看:106
本文介绍了解析.csv文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的任务是从Outlook导入联系人。

用户要以.csv格式导出文件我必须解析该数据并取名字来自该文件的姓氏和电子邮件。



部分原因是这个..我可以分成每行,然后我想分开逗号,但我得到一个我的for循环错误....

隐式转换是不可能的?????



不知道为什么???如何从该阵列获取名字和电子邮件详情????? /





Hi guys,
My task is to import contacts from Outlook.
user going to export the file in .csv format I have to parse that data and take firstname, lastname and Email from that file.

Part of that is this.. I am able to separate into each lines then I want to separate comma's but I am getting an error in my for loop....
implicit conversion is not possible?????

No idea why??? How can I get firstname and email details from that array?????/


using (Stream s = file.InputStream)
          {
              string[] numberOfUsers;
              var sr = new StreamReader(s);
              string contacts = sr.ReadToEnd();
              contacts = contacts.Replace("\r\n", "\n");
              string[] separateLines = contacts.ToString().Split('\n');
              for (int i = 0; i <= ((separateLines.Length) - 1); i++)
              {
                  numberOfUsers[i] = (separateLines[i + 1].ToString()).Split(',');
              }

推荐答案

foreach (string line in separateLines)
{
    numberOfUsers = line.Split(',');
    // now you can go through the field
    foreach( string fieldOnLine in numberOfUsers )
    {
        // process fieldOnLine here
    }
}





string.spilt()返回一个字符串[]而不是一个字符串,即numberOfUser [i]是一个字符串。 numberOfUser是一个字符串[]



string.spilt() return a string[] not a string i.e numberOfUser[i] is a string. numberOfUser is a string[]


您可能需要查看此工具, FileHelpers v2.0 - 定界(CSV)或固定数据导入/导出框架 [ ^ ]



您尚未初始化numberOfUsers数组,仅声明它。由于每个文件可以有可变数量的联系人,我会使用List< string>而不是一个数组。



为了将来参考,在这一行中,ToString不是必需的,因为seperateLines已经是一个字符串数组;



separateLines [i + 1] .ToString()
You may want to look at this tool, FileHelpers v2.0 - Delimited (CSV) or Fixed Data Import/Export Framework[^]

You have not initialized the numberOfUsers array, only declared it. Since there can be a variable number of contacts per file I would have used a List<string> rather than an array.

For future reference, in this line the ToString is not necessary since seperateLines is already a string array;

separateLines[i + 1].ToString()


这篇关于解析.csv文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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