473,785 Members | 2,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing CSV data into multidimensiona l 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.ReadL ine();
if (sLine != null)
//add each line of text file in
arrText.Add(sLi ne);
}
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 21872
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(o bject sender, System.EventArg s e)
{
// Split line on commas followed by zero or more spaces.
Regex splitRx = new Regex(@",\s*", RegexOptions.Co mpiled);
ArrayList al = new ArrayList();
using(StreamRea der sr = new StreamReader(@" c:\mycsv.csv"))
{
string line = null;
int ln = 0;
while((line = sr.ReadLine()) != null)
{
string[] fields = splitRx.Split(l ine);
if ( fields.Length != 2 )
{
Console.WriteLi ne("Invalid Input on line:"+ln);
continue;
}
ln++;
al.Add(fields);
}
}

Console.WriteLi ne("\nI processed {0} lines:", al.Count);
foreach(string[] sa in al)
{
Console.WriteLi ne("[{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******@discu ssions.microsof t.com> wrote in message
news:83******** *************** ***********@mic rosoft.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.ReadL ine();
if (sLine != null)
//add each line of text file in
arrText.Add(sLi ne);
}
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
4445
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 sorted columns. to do this at the moment i am using perl, which is very easy to do, and i want to see if python is as easy. so, the data i am using is some epiphyte population abundance data for particular sites, and it looks like this:
15
3631
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," "); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.");
2
12836
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, comparing the text in Label3 and LblThuTotal and the text in Label4 and LblFriTotal.
6
3185
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 entries with the entry in the other table? Having a separate table would result in two queries instead of one, but you wouldn't have to deal with the overhead of serializing and unserializing data. -- Kyle
1
1613
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 manipulate the position of each "currentfield" to then imput this data to another source as a string. The delimited text might look like the following. acctno, name, expdate,cr_line 4428123456789012,smith,0807,5000 4428123456789025,williams,0908,15000
3
4602
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. My SQL result comes with something like this: Africa | y2000 | 9430 Africa | y2005 | 9678 Europe | y2000 | 2314 where there are 6 regions and multiple years.
5
2321
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 =31 )
1
2207
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 form pages, well over 500 potential fields, which are written in PHP. While most pages are one time entry forms, there are 5 that can be “recycled” as many times as needed. An example would be the Contacts Form. A user can give me 1 contact and move...
9
4502
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 function: x = new int;
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10093
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.