473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Parsing CSV data into multidimensional array

Hi folks,

New C# programmer here.

I am reading some CSV data from a file into an ArrayList. I want to get the
data from the ArrayList into a 2-dimensional array. I see a few references to
..Split, but I'm not sure that's what I need.

So, basically, what I have loaded into the ArrayList is:

"a, 1"
"b, 2"
"c, 3"

And what I want is a 2-d array with

array[0,0] == "a"
array[0,1] == "1"
array[1,0] == "b" etc.

Here's my code so far:

static void Main(string[] args)
{
//open streamreader to read in data file
StreamReader objReader = new StreamReader("c:\\data.csv");
string sLine="";

//read file in as an arraylist
ArrayList arrText = new ArrayList();

//read until end of file
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
//add each line of text file in
arrText.Add(sLine);
}
objReader.Close();

//create the 2-d array
string [ , ] mydata;

//parse out the columns
//mystery code goes here
You guys are great. Thanks in advance for any help/advice.

Dave

Nov 16 '05 #1
1 21814
Here is one way I would do it. I use an ArrayList as I don't know before
hand how many lines I will parse:

private void button9_Click(object sender, System.EventArgs e)
{
// Split line on commas followed by zero or more spaces.
Regex splitRx = new Regex(@",\s*", RegexOptions.Compiled);
ArrayList al = new ArrayList();
using(StreamReader sr = new StreamReader(@"c:\mycsv.csv"))
{
string line = null;
int ln = 0;
while((line = sr.ReadLine()) != null)
{
string[] fields = splitRx.Split(line);
if ( fields.Length != 2 )
{
Console.WriteLine("Invalid Input on line:"+ln);
continue;
}
ln++;
al.Add(fields);
}
}

Console.WriteLine("\nI processed {0} lines:", al.Count);
foreach(string[] sa in al)
{
Console.WriteLine("[{0}] [{1}]", sa[0], sa[1]);
}
}

------- mycsv.csv File ---------
a,1
b,2
c, 3
d, 4
--
William Stacey, MVP
http://mvp.support.microsoft.com

"davehunt" <da******@discussions.microsoft.com> wrote in message
news:83**********************************@microsof t.com...
Hi folks,

New C# programmer here.

I am reading some CSV data from a file into an ArrayList. I want to get the data from the ArrayList into a 2-dimensional array. I see a few references to .Split, but I'm not sure that's what I need.

So, basically, what I have loaded into the ArrayList is:

"a, 1"
"b, 2"
"c, 3"

And what I want is a 2-d array with

array[0,0] == "a"
array[0,1] == "1"
array[1,0] == "b" etc.

Here's my code so far:

static void Main(string[] args)
{
//open streamreader to read in data file
StreamReader objReader = new StreamReader("c:\\data.csv");
string sLine="";

//read file in as an arraylist
ArrayList arrText = new ArrayList();

//read until end of file
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
//add each line of text file in
arrText.Add(sLine);
}
objReader.Close();

//create the 2-d array
string [ , ] mydata;

//parse out the columns
//mystery code goes here
You guys are great. Thanks in advance for any help/advice.

Dave


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
15
by: John Smith | last post by:
I would like to parse a string into an array. I found on the net the following codes which parse a string and print it. The result is exactly what I want: char * pch; pch = strtok (buffer," ");...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
1
by: Necromis | last post by:
I am trying to figure out a way to change the below code to store data in a multidemensional array rather than a listbox. The reason being is I need to be able to pull each "currentrow" and...
3
by: luftikus143 | last post by:
Hi there, I need to store three pieces of data - a result of a SQL query - in a multidimensional array, but don't really succeed. Sometimes it works, but then the output doesn't work accordingly....
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
1
nine72
by: nine72 | last post by:
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance… I will try my best to describe what I have going on… 1) I have 15...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.