473,320 Members | 1,950 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,320 software developers and data experts.

CVS Text File

I have a large text file seperated by commas. I was wondering how to get the
values from the text file. The problem is that not all the characters are
the same length, so I can't use the characters, but is I can't find how to
seperate out the values by the commas. I know that reading the whole line is
possible, but what if I just want the third value and not the rest? Thanks.

exp: 9821,1550,200,2004
Nov 21 '05 #1
3 1375
This should help you

Dim fs As FileStream
Dim sr As StreamReader

Try
Dim line As String
Dim fields As String()
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs)
line = sr.ReadLine()
While Len(line) > 0
fields = Split(line, ",")
For Each field As String In fields
Debug.WriteLine(field)
Next
line = sr.ReadLine()
End While
Catch ex As Exception
' display error
Finally
If Not IsNothing(fs) Then
fs.Close()
sr.Close()
End If
End Try

"Kou Vang" <Ko*****@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
I have a large text file seperated by commas. I was wondering how to get the values from the text file. The problem is that not all the characters are
the same length, so I can't use the characters, but is I can't find how to
seperate out the values by the commas. I know that reading the whole line is possible, but what if I just want the third value and not the rest? Thanks.
exp: 9821,1550,200,2004

Nov 21 '05 #2
"Kou Vang" <Ko*****@discussions.microsoft.com> schrieb:
I have a large text file seperated by commas. I was wondering how to get
the
values from the text file. The problem is that not all the characters are
the same length, so I can't use the characters, but is I can't find how to
seperate out the values by the commas. I know that reading the whole line
is
possible, but what if I just want the third value and not the rest?
Thanks.

exp: 9821,1550,200,2004


You can use 'StreamReader' to read the line and split it up into its parts
using 'Split' (specify '","c' as splitting character). Then you can easily
pick the third part from the array returned by 'Split'.

Alternatively you can use the text driver to read a CSV file:

<URL:http://www.connectionstrings.com/>
-> "Text"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
On Tue, 14 Dec 2004 08:39:06 -0800, "Kou Vang" <Ko*****@discussions.microsoft.com> wrote:

¤ I have a large text file seperated by commas. I was wondering how to get the
¤ values from the text file. The problem is that not all the characters are
¤ the same length, so I can't use the characters, but is I can't find how to
¤ seperate out the values by the commas. I know that reading the whole line is
¤ possible, but what if I just want the third value and not the rest? Thanks.
¤
¤ exp: 9821,1550,200,2004

You can use ADO.NET and the OLEDB provider to do this:

Dim ConnectionString As String

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "E:\My Documents\TextFiles" & ";" & _
"Extended Properties=""Text;HDR=NO;"""

Dim TextConnection As New System.Data.OleDb.OleDbConnection(ConnectionString )
TextConnection.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM CSV#txt", TextConnection)

Dim ds As New DataSet("TextFiles")
da.Fill(ds, "CSV")

Dim dt As DataTable

dt = ds.Tables("CSV")

DataGrid1.SetDataBinding(ds, "CSV")

Dim drCurrent As DataRow
For Each drCurrent In dt.Rows
'Console.WriteLine(drCurrent.Item(0).ToString)
'Console.WriteLine(drCurrent(1).ToString)
Console.WriteLine(drCurrent(2).ToString)
'Console.WriteLine(drCurrent(3).ToString)
'Console.WriteLine(drCurrent(4).ToString)
'Console.WriteLine(drCurrent(5).ToString)
'Console.WriteLine(drCurrent(6).ToString)
Next

TextConnection.Close()
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #4

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

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
by: Rigga | last post by:
Hi, I am new to Python and need to parse a text file and cut parts out i.e. say the text file contained 5 rows of text: line 1 of the text file line 2 of the text file line 3 of the text...
27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
16
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
7
by: Chris | last post by:
Hi I can use a text file as a datasource but am unable to get the datatable to see the text file as having multiple columns. Everything gets put into the first column in the datatable. Sample of...
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
1
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.