Read Csv file with LineFeeds within its fields

China☆狼群 提交于 2020-01-11 11:33:07

问题


I have this code to read a csv file :

Dim strLineValue As String
Using sr As StreamReader = File.OpenText("FilePath")

strLineValue = sr.ReadLine
      Do While strLineValue IsNot Nothing
          strLineValue = sr.ReadLine
           n += 1
      Loop
End Using

My problem is that I come across a csv file that the lines is like this:

 "Text1 LF LF text2","text3",text4,text5, , , , ,LF 
 "Text6 LF LF text8","text9",text10,text11, , , , ,LF 

where LF is Line Feed.

So I get something like this which is wrong

Text1

text2    text3    text4    text5
Text6

text8    text9    text10   text11

Any ideas how I can overcome this wrong behaviour of my code in this type of files

PS. 1.If I open the csv file in excel it recognises the lines properly , it just have a multiline first cell
2. I am thinking that maybe the first 2 LF are just LF and the LF that I have in the end of each line are LF and CR but how I can see the difference (I opened the csv file in the Word to see the characters)


回答1:


You have some fields that are enclosed in double-quotes - ". In CSV files, that usually indicates that you're supposed to take the whole field and not parse it.

This is really easy to do with the Microsoft.VisualBasic.FielIO.TextFieldParser class. Here's an example:

Imports Microsoft.VisualBasic.FileIO

Dim parser As TextFieldParser = New TextFieldParser("TestFile.txt")

parser.Delimiters = New String() {","}
parser.HasFieldsEnclosedInQuotes = True

While Not parser.EndOfData

    Dim fields As String() = parser.ReadFields()

End While

This will preserve the line feeds in the quoted fields:

"Text1 LF LF text2" "text3" "text"4 "text5" blank blank blank blank blank



回答2:


I would try;

strLineValue = strLineValue.replace(vblf,"")

And see it indeed the end of the line has a CR...

You can see the difference in a HEX editor, a lf = 10 and a cr=13

chr(10) & chr(13) = vbcrlf


来源:https://stackoverflow.com/questions/18206487/read-csv-file-with-linefeeds-within-its-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!