Posts Topics Forums Images
Search videos from message boards Videos Search messages from microblogs Microblogs Search messages from imdb.com Imdb Search messages from yuku.com Yuku Search messages from lefora.com (free forums) Lefora
My account: Login | Sign Up
Loading... 

Thread: Reading a .CSV file

Started 1 month, 2 weeks ago by GAMinTN
Good Morning! I have a vb.net POS application which creates a CSV file (three actually) to store data for later import into a database.  All is working well with that part.  Details and what I would like to do now follows: A single row of data in one of these files looks like this: 11/4/2009 1:57:50 PM,fg5,1234,21.00,21,0,0,0,0,21.00,12345 There are 11, I will call them columns, ...
Site: MSDN Forums  MSDN Forums - site profile
Forum: Visual Basic General  Visual Basic General - forum profile
Total authors: 10 authors
Total thread posts: 35 posts
Thread activity: no new posts during last week
Domain info for: microsoft.com

Other posts in this thread:

Super Dave Osborne replied 1 month, 2 weeks ago
Have you looked at all the .csv libraries and links for linq and .csv?  Seems the easiest way to handle .csv's. Just google linq .csv. There is a codeproject lib and a lot of links. You could also streamread the .csv and do a for each and split each line into a string array with split() then just take index 10 of your string array. Proposed As Answer by Duckster101 13 hours 37 ...

jwavila replied 1 month, 2 weeks ago
using TextFieldParser, and parsing each line into an array, the 10th column will be item 9 in the array. The string to double just means you have to use something like CDbl to convert the string from the file - but only use it if you are sure the data is valid. If not, use Double.TryParse. Dim filename As String = "C:\Documents and Settings\AVILAJ\Desktop\Test Folder\GAMinTN.csv"...

Derek Belanger replied 1 month, 2 weeks ago
My favorite way to work with delimited files is by using the built-in IDE snippits. Right Click --> Insert Snippit --> Fundamentals --> File System --> Read Delimited Text File ...will insert the following code Safely handles file access and its very fast. Dim filename As String = "C:\Test.txt" Dim fields As String () Dim delimiter As String = ","...

Super Dave Osborne replied 1 month, 2 weeks ago
Have you looked at all the .csv libraries and links for linq and .csv?  Seems the easiest way to handle .csv's. Just google linq .csv. There is a codeproject lib and a lot of links. You could also streamread the .csv and do a for each and split each line into a string array with split() then just take index 10 of your string array. Proposed As Answer by Duckster101 Friday, ...

JohnWein replied 1 month, 2 weeks ago
Your file is a string separated by newline characters and commas.  There is no way to know where column 10 is without parsing the file.  The quickest way to parse small csv files of 100 MB or less is to ReadAllLines and then split on the commas.  Larger files will have to be read line-by-line and then split.

Duckster101 replied 1 month, 2 weeks ago
Dim Total As Double   Dim lcStringArray() As String = "11/4/2009 1:57:50 PM,fg5,1234,21.00,21,0,0,0,0,21.00,12345".Split(", ")   Dim total1 As Double = lcStringArray(9)   Total = Total + Total1

Tom Shelton replied 1 month, 2 weeks ago
Using the VB file functions is an exercise in futility.  They are extremely slow.  I would seriously consider looking at using System.IO for your file work. As for the rest of the question - because this is not a fixed field file, there is no way to magically get to column 10.  You simply have to read in all of the records and extract the data you want.  I would begin with code that looked ...

Super Dave Osborne replied 1 month, 2 weeks ago
http://msdn.microsoft.com/en-us/library/bb882644.a spx You might look at that link too for a linq example.

Tom Shelton replied 1 month, 2 weeks ago
Yes, you could use linq...  But I see a couple of issues with doing so.   If the file is large, and you read the whole thing into memory that may cause issues.  If you wish to stream the file results, it becomes a much harder problem in VB because of it's poor iterator support.   In C# you can do something like: internal static class StreamReaderExtension { public static...

DeborahK replied 1 month, 2 weeks ago
You could try something like this: http://msmvps.com/blogs/deborahk/archive/2009/08/2 5/reading-comma-delimited-files-textfieldparser.as px Which uses TextFieldParser. Hope this helps.

 

Top contributing authors

Name
Posts
GAMinTN
11
user's latest post:
Reading a .CSV file
Published (2009-11-24 16:16:00)
I checked, each line in the CSV file does end with a comma. Latest issue - application is reading CSV file but only first line.  Not sure how to get it to move to the next line and continue until it reaches the end of the file. Clarification - App reads first line over and over again.  It seems to be stuck in the "While" loop without moving to the next line of data.  Total value increases as line 1 is added to previous...
Tom Shelton
7
user's latest post:
Reading a .CSV file
Published (2009-11-13 19:12:00)
LOL.. Gal, it's been a long time :)  The IsNot operator was introduced in 2005 as well, so you need to do the tests the old way like this: Option Strict On Option Explicit On Imports System Imports System.IO Module Module1 Sub Main() Dim total As Decimal = 0 Dim reader As StreamReader Try reader = New StreamReader( "demo.csv" ) Dim line As String = reader.ReadLine() While Not line Is Nothing Dim fields() As String =...
JohnWein
4
user's latest post:
Reading a .CSV file
Published (2009-11-13 21:51:00)
Try Finally End Try  ?
Derek Belanger
3
user's latest post:
Reading a .CSV file
Published (2009-11-13 18:45:00)
Don't try most of the answers in VB2003.  Most forum responders think in VB8,9 and later.  Look in your help for how to read and write text files in Visual Basic. Oh my! Yes, this is an important detail many of us overlooked. 2003 was a long time ago :) I think the way I used to do it was using streamreader.readline() and string.split()
Super Dave Osborne
3
user's latest post:
Reading a .CSV file
Published (2009-11-13 15:23:00)
http://msdn.microsoft.com/en-us/library/bb882644.aspx You might look at that link too for a linq example.
jwavila
2
user's latest post:
Reading a .CSV file
Published (2009-11-13 17:35:00)
using TextFieldParser, and parsing each line into an array, the 10th column will be item 9 in the array. The string to double just means you have to use something like CDbl to convert the string from the file - but only use it if you are sure the data is valid. If not, use Double.TryParse. Dim filename As String = "C:\Documents and Settings\AVILAJ\Desktop\Test Folder\GAMinTN.csv" Private Sub Button1_Click( ByVal sender As...
John Anthony Oliver
2
user's latest post:
Reading a .CSV file
Published (2009-11-24 16:44:00)
Hi George, With the example CSV file I tried the following. Add 1 Button to a FORM to try this code please. This code should be okay for VB.Net 2003, from what I remember. Please read the code comments and change the Dim CsvFilePath As String = "C:\Tmp\" '<< Don't forget the "\" at the end!! and Dim CsvFileName As String = "Demo.csv" as appropriate. :-) Regards,...
Duckster101
1
user's latest post:
Reading a .CSV file
Published (2009-11-13 15:10:00)
Dim Total As Double   Dim lcStringArray() As String = "11/4/2009 1:57:50 PM,fg5,1234,21.00,21,0,0,0,0,21.00,12345".Split(",")   Dim total1 As Double = lcStringArray(9)   Total = Total + Total1
Paul P Clement IV
1
user's latest post:
Reading a .CSV file
Published (2009-11-13 19:23:00)
You can still use data access methods but if you plan on supporting 64-bit in the near future I would recommend the file I/O methods mentioned. Dim ConnectionString As String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Documents and Settings\...\My Documents\My Database\Text;" & _ "Extended...
DeborahK
1
user's latest post:
Reading a .CSV file
Published (2009-11-13 17:01:00)
You could try something like this: http://msmvps.com/blogs/deborahk/archive/2009/08/25/reading-comma-delimited-files-textfieldparser.aspx Which uses TextFieldParser. Hope this helps.

Related threads on "MSDN Forums":

Related threads on other sites:

Thread profile page for "Reading a .CSV file" on http://msdn.microsoft.com. This report page is a snippet summary view from a single thread "Reading a .CSV file", located on the Message Board at http://msdn.microsoft.com. This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity