Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Read CSV file into internal table

former_member220801
Participant
0 Likes
26,024

I have a CSV file with one of the line

"abc","aaa,ab",10,"bbc"

At first, I used

split str at ',' into table itab

and then

replace all the """ with an empty string

But obviously that my case will be failed as one of the field contains "," in its content.

I would like to ask where there is some functions in SAP can can read a CSV file into internal table. Thanks!

1 ACCEPTED SOLUTION
Read only

Former Member
9,709

hi Gundam,

you can do something like this..

DATA: BEGIN OF itab OCCURS 0,
        vbeln LIKE vbak-vbeln,
        ernam LIKE vbak-ernam,
      END OF itab.
 
DATA itab2 LIKE TABLE OF KCDE_CELLS WITH HEADER LINE.
 
CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
  EXPORTING
    i_filename            = 'D:dataupl.txt'
    i_separator           = ','
  tables
    e_intern              = itab2
* EXCEPTIONS
*   UPLOAD_CSV            = 1
*   UPLOAD_FILETYPE       = 2
*   OTHERS                = 3
          .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 
LOOP AT itab2.
  SPLIT itab2-value AT ',' INTO itab-vbeln itab-ernam.
  APPEND itab.
  CLEAR itab.
ENDLOOP.

regards

satesh

I have a CSV file with one of the line

"abc","aaa,ab",10,"bbc"

At first, I used

split str at ',' into table itab

and then

replace all the """ with an empty string

But obviously that my case will be failed as one of the field contains "," in its content.

I would like to ask where there is some functions in SAP can can read a CSV file into internal table. Thanks!

7 REPLIES 7
Read only

Former Member
0 Likes
9,709

Hi,

chk this FM KCD_CSV_FILE_TO_INTERN_CONVERT

Read only

Former Member
0 Likes
9,709

Hi

Try fm TEXT_CONVERT_CSV_TO_SAP

Anyway it should be better you re-format your csv file by excel to delete ".

Max

Read only

Former Member
9,710

hi Gundam,

you can do something like this..

DATA: BEGIN OF itab OCCURS 0,
        vbeln LIKE vbak-vbeln,
        ernam LIKE vbak-ernam,
      END OF itab.
 
DATA itab2 LIKE TABLE OF KCDE_CELLS WITH HEADER LINE.
 
CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
  EXPORTING
    i_filename            = 'D:dataupl.txt'
    i_separator           = ','
  tables
    e_intern              = itab2
* EXCEPTIONS
*   UPLOAD_CSV            = 1
*   UPLOAD_FILETYPE       = 2
*   OTHERS                = 3
          .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 
LOOP AT itab2.
  SPLIT itab2-value AT ',' INTO itab-vbeln itab-ernam.
  APPEND itab.
  CLEAR itab.
ENDLOOP.

regards

satesh

Read only

Former Member
0 Likes
9,709

Hi,

You can use this FM <b>KCD_CSV_FILE_TO_INTERN_CONVERT</b>

and get the CSV data to Intenal table.

Regards

Vijay

Read only

vinod_gunaware2
Active Contributor
0 Likes
9,709

Hi

Use First

<b>replace all the """ with an empty string</b>

then use <b>Condense with no-gaps</b>.

Then <b>split str at ',' into table itab</b>

It will be solve ur problem.

regards

vinod

Read only

former_member220801
Participant
0 Likes
9,709

Thanks for reply.

KCD_CSV_FILE_TO_INTERN_CONVERT seems only work for local file.

(1)But what if my file is from application server?

My expect output for the line in CSV

"abc","aaa,ab",10,"bbc"

=>

abc

aaa,ab

10

bbc

(2)for FM KCD_CSV_FILE_TO_INTERN_CONVERT, it seems to have some problem for the last field.

the result becomes

abc

aaa,ab

10

"bbc"

Read only

0 Likes
9,709

Hi

I think your CSV file is not correctly generated because there're , and " as separator.

So you should split your file twice:

if you have: "abc","aaa,ab",10,"bbc"

REFRESH ITAB.

SPLIT WA AT """ INTO TABLE ITAB.

Now ITAB should be filled in this way:

record 1: abc

record 2: ,

record 3: aaa,ab

record 4: ,10,

record 5: bbc

So you try to do something like fallowing code

LOOP AT ITAB.

CONDENSE ITAB-FIELD NO-GAPS.

IF ITAB-FIELD = ','.

DELETE ITAB.

ELSE.

DO.

REPLACE ',' WITH SPACE INTO ITAB-FIELD.

IF SY-SUBRC <> 0. EXIT. ENDIF.

ENDDO.

CONDENSE ITAB-FIELD NO-GAPS.

MODIFY ITAB.

ENDIF.

ENDLOOP.

Max