2006 Mar 29 9:40 AM
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!
2006 Mar 29 9:45 AM
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!
2006 Mar 29 9:44 AM
2006 Mar 29 9:45 AM
Hi
Try fm TEXT_CONVERT_CSV_TO_SAP
Anyway it should be better you re-format your csv file by excel to delete ".
Max
2006 Mar 29 9:45 AM
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
2006 Mar 29 9:47 AM
Hi,
You can use this FM <b>KCD_CSV_FILE_TO_INTERN_CONVERT</b>
and get the CSV data to Intenal table.
Regards
Vijay
2006 Mar 29 10:04 AM
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
2006 Mar 29 10:15 AM
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"
2006 Mar 29 10:27 AM
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
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |