473,399 Members | 4,254 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,399 software developers and data experts.

how to read a csv file using c#

1
how to read a csv file using c#
Sep 2 '08 #1
2 1946
Plater
7,872 Expert 4TB
You will need to write a function to parse the file. I recomend looking up a regular expression that can do it, and then implementing THAT.
Sep 3 '08 #2
balabaster
797 Expert 512MB
You will need to write a function to parse the file. I recomend looking up a regular expression that can do it, and then implementing THAT.
For sure one way would be a regular expression, but my opinion is this:

A CSV file is really just a plaintext file...each line represents a record, each record's fields are separated by a comma ",".

Before you can parse the file, you need to answer yourself the question, can fields contain commas, and if so, what in the file denotes that a field contains a comma. In most cases, a field can contain a comma if it's wrapped with single or double quotes.

If your fields are not going to be allowed to contain commas, then it's as simple as splitting each row based on ",".

Expand|Select|Wrap|Line Numbers
  1. Dim oRdr As System.IO.StreamReader = System.IO.File.OpenText("C:\Test\MyTestFile.csv")
  2.   Dim CrntLine As String = Nothing
  3.   While CrntLine = oRdr.ReadLine
  4.   Dim Fields() As String = CrntLine.Split(",")
  5. End While
Now you can treat Fields() as any other array, each of your fields is held within an item in the array.

If you need your fields to be able to contain commas when wrapped, then you'll have to get a bit more complex...

In order to allow for a field to contain a comma, you'd need to parse each field individually.

Expand|Select|Wrap|Line Numbers
  1. While crntLine <> ""
  2.  
  3.   Dim FieldStart As Integer = 0
  4.   Dim FieldEnd As Integer = 0
  5.   Dim NextField As Integer = 0
  6.  
  7.   If crntLine.IndexOf(""") = 0 Then
  8.     FieldStart = 1 'We don't care to include the "
  9.     'Handle a closing " that isn't followed immediately by a ,
  10.     'What we're doing here is finding the closing ", but then reading
  11.     'on to the next , and treating it all as one field.
  12.     Dim CloseIndex As Integer = crntLine.IndexOf("""", 1) - 1
  13.     FieldEnd = crntLine.IndexOf(",", CloseIndex)
  14.   Else
  15.     FieldStart = 0
  16.     FieldEnd = crntLine.IndexOf(",")
  17.   End If
  18.  
  19.   crntField = crntLine.SubString(FieldStart, FieldEnd)  'Pass this out to do something useful...
  20.  
  21.   'Remove the current field from the string
  22.   crntLine = crntLine.SubString(FieldEnd + 1) '+1 drops the trailing ,
  23.  
  24. End While
Bear in mind that while the second method may be more encompassing, because it has to parse each line more thoroughly, it may be noticeably slower.
Sep 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
14
by: Mark Broadbent | last post by:
Does anybody know what is (factual please -not just guess) the quickest method to read data from a file? I am not interested in the format of the data (i.e. blocks, bytes, string etc) just that the...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
5
by: JenHu | last post by:
Hi experts, I wrote a function which retrieves a file in the folder, the file path is : Dim sr As New StreamReader(strFilepath & ReturnFileName) What if I have more than 1 file_name in...
8
by: james | last post by:
I am trying to use Filestream to read a file ( .DAT) that contains values in HEX that I want to convert to text. I know the different offset addresses for each portion of the data I am trying to...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
3
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another...
3
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.