csv 是否将分号分隔的字符串转换为表结构?

taor4pac  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(140)

我需要一些关于在ITAB中将字符串转换为的帮助。

LOOP AT LT_0111_FILE INTO LV_STRING.
    SPLIT LV_STRING AT ';' INTO TABLE LT_0111.
    DO GV_COMP TIMES.
      READ TABLE LT_0111 ASSIGNING <LV> INDEX SY-INDEX.
      IF <LV> IS NOT INITIAL.
        ASSIGN COMPONENT SY-INDEX OF STRUCTURE <STRUCT> TO <LV_COMP>.
        IF <LV_COMP> IS ASSIGNED.
          <LV_COMP> = <LV>.
        ENDIF.
      ENDIF.
    ENDDO.
    INSERT <STRUCT> INTO TABLE <TABLE>.
  ENDLOOP.

在LT_0111_FILE中,表格PA 0111为带有分隔符“的字符串;“。现在我需要将该字符串表的每个字段分配到PA 0111结构的字段中。
我不想对每个字段单独执行此操作,因为字段将动态创建。
此代码适用于字符字段,但不适用于数字。在txt文件中,将存在诸如0,00之类的数字,将它们移动到结构的字段中将给予错误,因为数字必须为0.00。
谢谢你的帮助

gjmwrych

gjmwrych1#

当您有一个未知的结构,并且想知道它有哪些字段以及这些字段有哪些属性时,可以使用runtime type information类。
首先,获取目标结构的类型描述。

DATA lo_struct_description TYPE REF TO cl_abap_structdescr.
lo_struct_description ?= cl_abap_typedescr=>describe_by_data( <struct> ).

这里需要强制转换操作符?=,因为describe_by_data的返回值是一个泛型cl_abap_typedescr。* 您 * 知道它必须是一个结构,但类不知道这一点。它也可以是一个表、简单类型或对对象的引用。但如果您能保证它必须是一个结构,则可以将其上强制转换为cl_abap_structdescr
现在你可以得到一个描述结构中所有字段的表:

DATA lt_components TYPE abap_component_tab.
lt_components = lo_struct_description->get_components( ).

这个表包含了这些组件的名称和类型,因此,您可以LOOP AT组件表并按名称使用ASSIGN COMPONENT,而不是使用DO循环并按索引使用ASSIGN COMPONENT,然后您可以根据类型处理每个字段:

LOOP AT lt_components INTO ls_component.
  READ TABLE lt_0111 ASSIGNING <lv_in> INDEX sy-tabix.
  IF sy-subrc = 0.
    ASSIGN COMPONENT ls_component-name OF STRUCTURE <struct> TO <lv_out>.
    IF sy-subrc = 0.
      CASE ls_component-type->type_kind.
        WHEN cl_abap_datadescr=>typekind_date.
          " Special handling for dates
        WHEN cl_abap_datadescr=>typekind_packed.
          " Special handling for decimal numbers
         
        " Check the other constants cl_abap_datadescr=>typekind_* for all the other types
        " you might encounter in your use-case and which might require special treatment.
        WHEN OTHERS.
          " Probably just copyable. If not, you will get a runtime error here and need 
          " to implement special handling for this particular type_kind.
          <lv_out> = <lv_in>.
      ENDCASE.
    ENDIF.
  ENDIF.
ENDLOOP.
thtygnil

thtygnil2#

事实上我更喜欢飞利浦的解决方案,它在类型处理方面更防错和全面,但只是为了多样性,我会添加这个快速和肮脏的解决方案。
可以使用cl_rsda_csv_converter辅助类的方法:

DATA: input TYPE TABLE OF string.

TYPES t_itab TYPE TABLE OF pa0009 WITH EMPTY KEY.
DATA(dref) = NEW t_itab(  ).

APPEND ` 800;90051099;0;;;99991231;20080501;000;20100312;HORVATLU;;;;;;;;;;;;;;;46456.89;HUF;0.00;;0;;;;;HU;;;;;;;;;;;;;;;;00;;;;00000000;;;;; ` TO input.
APPEND ` 800;99000005;0;;;99991231;20170101;000;20170220;GUNASHMA;;;;;;;;;;;;;;;5564665.00;EUR;0.00;;0;;;;;DE;28511111;123;;;;;;;;;;;;;;00;;;;00000000;;;;; ` TO input.

DATA: ls_line TYPE LINE OF t_itab.
LOOP AT input ASSIGNING FIELD-SYMBOL(<fs_s>).
* creating converter
  DATA(lo_csv) = cl_rsda_csv_converter=>create( i_separator = ';' ).
* Process records
  lo_csv->csv_to_structure( EXPORTING i_data = <fs_s> IMPORTING e_s_data = ls_line ).
* inserting into itab
  INSERT ls_line INTO TABLE dref->*.
ENDLOOP.

相关问题