Solved

How to delete CR LF in a .csv file using Vb.Net

Posted on 2014-09-23
6
829 Views
Last Modified: 2014-09-23
I am working on VS 2012, Vb.Net - .Net framework 4.0

This piece of code, read the .csv file and expected to rewrite the .csv file without Carriage Return.

But now, Carriage Return is created as a new line. I have also attached the screen shot.

Along with the post, i have attached the sample csv file.

 Public Sub Test()
        Try

            Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead("D:\CSV\Test.csv"))
            Dim listA As New List(Of String)()


            If File.Exists("d:\CSV\TestOut1.csv") Then
                File.Delete("d:\CSV\TestOut1.csv")
            End If

            Dim sw As New StreamWriter("d:\CSV\TestOut1.csv")
            Dim s As String = String.Empty

            While reader.Peek() >= 0
                Dim line As String = reader.ReadLine()
                Dim values As String() = line.Split(";"c)
                listA.Add(values(0))
                s = s + line + Chr(10)
            End While
            reader.Close()
            sw.WriteLine(s)
            sw.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

Open in new window

D--CSV-Expected-Output.jpg
D--CSV-Test.csv
0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:skipper68
ID: 40339633
have you tried .replace(vbcrlf, "") on the 'line' or the split array of items?
Dim values As String() = line.Split(";"c).replace(vbCrLf,"")
or
listA.Add(values(0).replace(vbCrLf,""))
0
 
LVL 14

Expert Comment

by:Don Thomson
ID: 40339639
search for the ascii character combination of CRLF  (13 and 10) and and don't write a new line

Or simply search for a  line with a length of less than 3 characters and ignore it

Not sure why your doing this - a .csv  file will always be created with the CRLF  at the end.
Every program that I've seen that imports a CSV file uses the CRLF as an end of file and stops writing
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40339670
Change this line of code

sw.WriteLine(s)

to this

sw.Write(s)

And see if that is what you are looking for.
0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 

Author Comment

by:chokka
ID: 40339840
@Fernando, Great !! Thank you !! Looks like it works !!

I need to test again to confirm
0
 

Author Closing Comment

by:chokka
ID: 40340482
Thank you !!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40340542
Not a problem, glad I was able to help.
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question