In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to convert csv data into DataTable in C#. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Pandas dataframe of python can directly convert csv data or formatted data (excel) into dataframe, a table-like data, so that in data processing, the data can be analyzed and processed quickly according to the set function.
The datatable of C # also converts the data into table form, which is commonly used in the C # operation database to read the data directly to datatable. Of course, the csv data can be imported into the database, and then the corresponding datatable can be obtained by connecting to the database.
The following is mainly about how to read an csv data with code and convert it to datatable for processing. For more information, please see the code:
/ / sets the delimiter for csv data
String [] separators = new string [] {""}
String _ dataLine = string.Empty
/ / create a new datatable
DataTable _ ndt = new DataTable ()
/ / Open the file
Using (FileStream fs = new FileStream (_ filepath, FileMode.Open, System.IO.FileAccess.Read))
{
/ / read csv data. What we should pay attention to here is to set the encoding of the data, otherwise it will be garbled when read.
Using (StreamReader sr = new StreamReader (fs, System.Text.Encoding.UTF8))
{
/ / the first row is the header data, which will be split into a column name
_ dataLine = sr.ReadLine ()
Var _ columnArr = _ dataLine.Split (separators, StringSplitOptions.RemoveEmptyEntries)
Foreach (var _ evarr in _ columnArr)
{
_ ndt.Columns.Add (new DataColumn (_ evarr.Trim (')
}
/ / add row data directly to datatable
While ((_ dataLine = sr.ReadLine ())! = null)
{
_ columnArr = _ dataLine.Split (separators, StringSplitOptions.RemoveEmptyEntries)
_ ndt.Rows.Add (_ columnArr)
}
Sr.Close ()
Sr.Dispose ()
}
Fs.Close ()
Fs.Dispose ()
}
Linq can also be used in datatable, so that the corresponding data can be easily extracted, such as a column of data:
Var _ selrow = _ ndt.AsEnumerable () .Select (p = > p ["column name"])
Thus, using the syntax of Linq, we can judge the maximum and minimum value, or take out the string that matches the name, and analyze the corresponding operation of the row data.
Linq in data analysis operations, can also achieve a lot, of course, python is open source class library and platform support is more.
On how to convert csv data into DataTable in C # is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.