本文介绍了文本字段解析器不会搜索完整的csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我不是全职程序员,所以请原谅我的编码标准。 我在csv中有大约51列和37000行。任务是将每个列解析为数组变量并与一组数据进行比较,如果满足条件则写入文本文件。 这个脚本的问题是,它不会遍历完整的csv文件。我得到的输出只是部分因为逻辑没有完成csv的完整迭代。迭代80%的csv文件后脚本完成,因此输出只是部分。 感谢你们是否可以对csv解析技术有所了解,并且可以深入了解脚本无法迭代完整csv的原因。 b $ b 使用 MyReader As 新 Microsoft.VisualBasic。 FileIO.TextFieldParser( C:\ Test Export2(2).csv) MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters( ,) Dim currentRow 作为 字符串() Dim Uniquecol As 新 ArrayList Dim temp As String = vbNullString Dim temp1 作为 字符串 = vbNullString 虽然 不 MyReader.EndOfData 尝试 currentRow = MyReader.ReadFields() 如果 CP_NAME.Equals(currentRow( 0 ))然后 objWriter.WriteLine( 什么到文件) ' objWriter.WriteLine () CP_Flag = True 结束 如果 如果 不 CP_Flag2 然后 如果 字符串 .Equals(CP_NAME,currentRow( 0 ))然后 ' objWriter.Write(什么到文件) objWriter.WriteLine( )' 插入新行 CP_Flag2 = True 结束 如果 结束 如果 如果 CP_NAME.Equals(currentRow( 0 )) 如果 字符串 .Equals(currentRow( 1 ),temp1)然后 GoTo Block_Option_Priority 其他 objWriter.WriteLine( MODIFY& & currentRow( 1 )) temp1 = currentRow( 1 ) GoTo Block_Option_Priority 结束 如果 如果 字符串 .Equals( currentRow( 5 ), STRING_CHECK )然后 选择 CASE (currentRow( 4 )) 案例 ABC 如果 字符串 .Equals(currentRow( 2 ),SOMESTRING)然后 如果 字符串 .Equals(currentRow ( 7 ),ZERO)然后 objWriter.WriteLine( TEST = 0) ElseIf 字符串 .Equals(currentRow( 7 ),ONE)然后 objWriter.WriteLine( TEST = 1) ElseIf 字符串 .Equals(currentRow( 7 ),TWO)然后 objWriter.WriteLine( TEST = 2) ElseIf 字符串 .Equals(currentRow( 7 ),THREE)然后 objWriter.WriteLine( TEST = 3) ElseIf String .Equals(currentRow( 7 ),FOUR)然后 objWriter.WriteLine( TEST = 4) ElseIf 字符串 .Equals(currentRow( 7 ),FIVE)然后 objWriter.WriteLine( TEST = 5) 结束 如果 结束 如果 如果 字符串 .Equals(currentRow( 2 ),SOME_SAMPLE_STRING)然后 ' 如果 字符串 .Equals( currentRow( 7 ),ZERO)然后 objWriter.WriteLine( TEST1 = 0) ElseIf 字符串 .Equals(currentRow( 7 ),ONE)然后 objWriter.WriteLine( TEST1 = 1) ElseIf String .Equals(currentRow( 7 ),TWO)然后 objWriter.WriteLine( TEST2 = 2) ElseIf String .Equals(currentRow( 7 ),THREE)然后 objWriter.WriteLine( TEST3 = 3 ) ElseIf 字符串 .Equals(currentRow( 7 ),FOUR)然后 objWriter.WriteLine( TEST4 = 4) ElseIf String .Equals(currentRow( 7 ),FIVE)然后 objWriter.WriteLine ( TEST5 = 5) End 如果 结束 如果 结束 选择 EndIf Catch ex 作为 Microsoft.VisualBasic。 FileIO.MalformedLineException MsgBox( Line& ex.Message& ; 无效,将被跳过。) 结束 尝试 结束 虽然 结束 使用 结束 Sub 解决方案 代码编写方式已经不值得修复。特别是,应该在一行代码中更改每个长if语句。什么线王?首先,我看不到你对ONE,TWO等的定义,最有可能的是,你不需要它们,其次,你没有准确地描述你的目标,最后,如果你自己不做,至少有一次,你最好忘记编程,无论一天多少小时(请参阅我对这个问题的评论)。 -SA I am not a full time programmer so excuse my coding standards.I have about 51 columns and 37000 rows in a csv. The task is to parse every column into an array variable and make comparison against a set of data, if conditions are met then make a write to a text file.The problem with this script is , it does not iterate through the full csv file. The output i get is only partial as the logic did not complete the full iteration of csv. The script finishes after iterating 80% of csv file, as a result the output is only partial.Appreciate if you guys can throw some light on csv parsing techniques and any insight into why the script fails to iterate the full csv.Using MyReader As New Microsoft.VisualBasic. FileIO.TextFieldParser( "C:\Test Export2 (2).csv") MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",") Dim currentRow As String() Dim Uniquecol As New ArrayList Dim temp As String = vbNullString Dim temp1 As String = vbNullString While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() If CP_NAME.Equals(currentRow(0)) Then objWriter.WriteLine("what ever to a file ") 'objWriter.WriteLine("") CP_Flag = True End If If Not CP_Flag2 Then If String.Equals(CP_NAME, currentRow(0)) Then 'objWriter.Write("what ever to a file") objWriter.WriteLine("") ' insert a new line CP_Flag2 = True End If End If If CP_NAME.Equals(currentRow(0)) If String.Equals(currentRow(1), temp1) Then GoTo Block_Option_Priority Else objWriter.WriteLine("MODIFY" & " " & currentRow(1)) temp1 = currentRow(1) GoTo Block_Option_Priority End IfIf String.Equals(currentRow(5), "STRING_CHECK") Then Select CASE (currentRow(4)) Case "ABC" If String.Equals(currentRow(2), SOMESTRING) Then If String.Equals(currentRow(7), ZERO) Then objWriter.WriteLine("TEST = 0") ElseIf String.Equals(currentRow(7), ONE) Then objWriter.WriteLine("TEST = 1") ElseIf String.Equals(currentRow(7), TWO) Then objWriter.WriteLine("TEST = 2") ElseIf String.Equals(currentRow(7), THREE) Then objWriter.WriteLine("TEST = 3") ElseIf String.Equals(currentRow(7), FOUR) Then objWriter.WriteLine("TEST = 4") ElseIf String.Equals(currentRow(7), FIVE) Then objWriter.WriteLine("TEST = 5") End If End If If String.Equals(currentRow(2), SOME_SAMPLE_STRING) Then ' If String.Equals(currentRow(7), ZERO) Then objWriter.WriteLine("TEST1 = 0") ElseIf String.Equals(currentRow(7), ONE) Then objWriter.WriteLine("TEST1 = 1") ElseIf String.Equals(currentRow(7), TWO) Then objWriter.WriteLine("TEST2 = 2") ElseIf String.Equals(currentRow(7), THREE) Then objWriter.WriteLine("TEST3 = 3") ElseIf String.Equals(currentRow(7), FOUR) Then objWriter.WriteLine("TEST4 = 4") ElseIf String.Equals(currentRow(7), FIVE) Then objWriter.WriteLine("TEST5 = 5") End If End If End SelectEndIf Catch ex As Microsoft.VisualBasic. FileIO.MalformedLineException MsgBox("Line " & ex.Message & "is not valid and will be skipped.") End Try End While End Using End Sub 解决方案 The code is written the way which already does not deserve fixing. In particular, each of the long "if" statements should be changed in one line of code. What king of line? First, I cannot see your definition of ONE, TWO, etc., most likely, you don't need them, secondly, you did not accurately describe your goal, and, finally, if you don't do it by yourself at least once, you should better forget programming, no matter how many hours a day (please see also my comment to the question).—SA 这篇关于文本字段解析器不会搜索完整的csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 21:41