使用文件助手解析多个字段 [英] parse several field using file helper

查看:27
本文介绍了使用文件助手解析多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在使用文件助手进行 CSV 解析时遇到问题.我的 CSV 看起来像这样

i have a problem regarding CSV parse using File Helper. My CSV is look like this

,,,026642,0,00336,05,19,"西瓜*",19,"1",,,,,,,,0,,001.99.,0,,,,,0,,0,0,,,,,,,,,,,,,,,,,,51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,026645,0,00338,05,19,"ONION",19,"*1",,,,,,,,0,,002.99.,0,,,,,0,,0,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,026687,0,00380,05,19,"蘑菇",19,"(黑木耳)",,,,,,,,0,,021.90.,0,,,,,0,,0,0,,,,,,,,,,,,,,,,,,51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

,,,026642,0,00336,05,19,"WATERMELON *",19,"1 ",,,,,,,,0,,001.99.,0,,,,,0,,0,0,,,,,,,,,,,,,,,,,,51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,026645,0,00338,05,19,"ONION ",19,"*1 ",,,,,,,,0,,002.99.,0,,,,,0,,0,0,,,,,,,,,,,,,,,,,,51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,026687,0,00380,05,19,"MUSHROOM ",19," (BLACK FUNGUS) ",,,,,,,,0,,021.90.,0,,,,,0,,0,0,,,,,,,,,,,,,,,,,,51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

这个 CSV 有 116 列/字段.

which this CSV have 116 column / field.

问题是我只需要整行中的 4 个字段

The problem was i just need only 4 field from whole line which

字段 4 = 026687,字段 9 = "西瓜 *",字段 11 = "(黑木耳)",字段 21 = 002.99.

Field 4 = 026687, Field 9 = "WATERMELON *", Field 11 = "(BLACK FUNGUS)", Field 21 = 002.99.

当我使用向导时,这是一个很好的创建实例类的解决方案,并且我将 FieldValueDiscarded _ 放在我不需要它的所有其他字段之上,它只是打印与 CSV 输入相同的内容.

When i using wizard which a very good solution to make instance class, and i put FieldValueDiscarded _ on top of every other fields i don't need it, it just print a same as CSV input.

请给我一些建议,我如何只提取需要的字段以写入输出.

Please give me some advise how do i extract only needed field to write into output.

谢谢

更新:经过更多研究,我终于弄清楚了这个错误.发生此错误是因为类不是继承的,因此我无法将特定字段放入另一个类.通过将映射类声明为继承类,我可以获得特定的字段.

UPDATED : After research some more, i finally getting clear about this error. This error happen because class non-inherited so i cannot get specific field into another class. By declare mapping class as inherited class, i can get specific field.

但是,我坚持如何检索从映射类继承的类中的函数.这是我的代码`导入 System.Text进口系统.IO导入文件助手

However, i stuck at how to retrieve functions inside the class who inherited from mapping class. This is my code `Imports System.Text Imports System.IO Imports FileHelpers

公共类 ProcessField : 继承 InputCSV

Public Class ProcessField : Inherits InputCSV

Public Function MyPLUc(ByVal value As Integer)
    Dim PLUc As String
    MyBase.PLU = value
    PLUc = (0 + 0 + value)
    Return (0 + 0 + value)
End Function
Public Function MyStatusc(ByVal value As Integer)
    MyBase.Status = value
    Dim Status As Integer
    If value > 0 Then
        Status = 800
    Else
        Status = 900
    End If
    Return (0 + Status)
End Function
Public Function MyUnitPricec(ByVal value As Integer)
    MyBase.UnitPrice = value
    Dim UP As Integer
    UP = value
    Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(UP)
    Return (0 + 0 + 0 + UP)
End Function
Public Function MyLabelFormat(ByVal value As Integer)
    Return (1 + 1)
End Function
Public Function MyEAN(ByVal value As Integer)
    Return (0 + 6)
End Function
Public Function MyCName(ByVal value As String)
    MyBase.CName1 = value
    Dim hexString As String = Hex(value)
    Return (hexString)
End Function
Public Function MyBCC(ByVal value As String)
    value = (0 + 0)
    Return (0 + 0)

End Function

结束类`

问题是我如何检索这些函数中的所有返回值

and the problem is how i retrieve all return value in these function

推荐答案

我不确定我是否理解你的问题,但我认为你让事情变得比你需要的更复杂.

I'm not sure I understand your question, but I think you are making things more complicated than you need.

您可以创建包含 116 个字符串字段的 FileHelpers 类.它们应该是(公共)字段,而不是属性.您不应该继承 FileHelpers 类.不要尝试将 FileHelpers 类用作普通类(即,具有属性、函数等、子类).仅将 FileHelpers 类视为 CSV 格式的规范".

You can just create your FileHelpers class with 116 string fields. They should be (public) fields, NOT properties. You should NOT subclass the FileHelpers class. Do not try to use the FileHelpers class as a normal class (i.e., with properties, functions, etc, subclasses). Think of the FileHelpers class as a 'specification' of your CSV format only.

要导入,您可以编写如下内容:

To import, you write something like:

Dim engine As New FileHelperEngine(GetType(RecordSpec)) 

' To Read Use: 
Dim results As RecordSpec() = DirectCast(engine.ReadFile("FileIn.txt"), RecordSpec()) 

然后你有一个 RecordSpec 的数组.每个 RecordSpec 将填充所有 116 个字段,但只需忽略您不需要的字段.然后遍历结果并对值执行任何您想要的操作.例如,您可能需要将导入的字段映射到另一个(更正常的)MyProduct 类,其中包含属性而不是字段,并且可能还需要附加逻辑.

Then you have an array of RecordSpec. Each RecordSpec will have all 116 fields populated, but just ignore the fields you don't need. Then loop through the results and do whatever you want with the values. For instance, perhaps you need to map the imported fields to another (more normal) MyProduct class with properties instead of fields and perhaps with additional logic.

For Each recordSpec As RecordSpec In results
  Dim myProduct = New MyProduct()
  myProduct.Id = recordSpec.Field4
  myProduct.Name = recordSpec.Field9
  myProduct.Category = recordSpec.Field23
  ' etc.
Next

总结:RecordSpec 类应该只用于定义 CSV 文件的结构.您可以使用它用文件中的所有值填充一个简单的数组.然后将这些值映射到一个更有用的类 MyProduct.

In summary: the RecordSpec class should only be used to define the structure of the CSV file. You use it to populate a simple array with all the values from the file. You then map the values to a more useful class MyProduct.

这篇关于使用文件助手解析多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆