‎2009 Apr 08 9:33 AM
Hi experts,
I need to save .csv from application server to internal table.
i am using the below code.
gt_raw and gwa_raw are dxrawdata format.
OPEN DATASET gv_pfile FOR INPUT IN TEXT MODE ENCODING DEFAULT.
*--- Display error messages if any.
IF sy-subrc NE 0.
WRITE:/ 'FILE UPLOAD FAILED - ERROR NO. : ', sy-subrc.
EXIT.
ELSE.
DO.
READ DATASET gv_pfile INTO gwa_raw.
IF sy-subrc NE 0.
EXIT.
ELSE.
APPEND gwa_raw TO gt_raw.
CLEAR gwa_raw.
ENDIF.
ENDDO.
*--Close the Application server file (Mandatory).
CLOSE DATASET gv_pfile.
ENDIF.
DELETE DATASET gv_pfile.
LOOP AT gt_raw into gwa_raw.
IF SY-TABIX > 1.
SPLIT gwa_raw at ',' into gwa_cust-cust_code
gwa_cust-cust_name
gwa_cust-grp_name
APPEND gwa_cust TO gt_cust.
CLEAR: gwa_cust, gwa_raw.
ENDIF.
ENDLOOP.
My program works fine.
But when the gwa_cust-grp_name contains the value for eg. -> panasonic co., ltd.
it takes till panasonic co., only
and leaves ltd. as i am using SPLIT command.
is there any other way to do this.
plz help me to solve this issue.
thanks.
‎2009 Apr 08 11:28 AM
Hi,
I notice you have marked the message as answered, but I just wanted to let you know there is a solution. The trick is to parse into an internal table and then to find and reassemble fields that were split because they contgain a comma. The ABAP program below is a commented example.
Rgds,
Mark
REPORT zcsv_parse.
DATA:
tokens TYPE i.
TYPES: BEGIN OF ty_result,
company TYPE char20,
compnr TYPE i,
city TYPE char30,
country TYPE char30,
END OF ty_result.
DATA:
gt_rawtab TYPE TABLE OF string,
gw_rawtab LIKE LINE OF gt_rawtab,
gt_result TYPE TABLE OF ty_result,
gw_result LIKE LINE OF gt_result,
gt_parse TYPE TABLE OF string,
gw_parse LIKE LINE OF gt_parse.
DEFINE %csvline.
gw_rawtab = &1.
append gw_rawtab to gt_rawtab.
END-OF-DEFINITION.
START-OF-SELECTION.
* Create CSV lines, some with a comma inside a token
%csvline '"CompanyOne NV",500,"Antwerp","Belgium"'.
%csvline '"CompanyTwo,Inc",600,"New York,NY","USA"'.
%csvline '"CompanyThree,Ltd",700,"Sydney,NSW","Australia"'.
* Parse the raw CSV
LOOP AT gt_rawtab INTO gw_rawtab.
REFRESH gt_parse.
SPLIT gw_rawtab AT ',' INTO TABLE gt_parse.
DESCRIBE TABLE gt_parse LINES tokens.
* If extra commas: token count higher than field count
IF tokens > 4.
PERFORM reassemble.
ENDIF.
* At this point each entry in GT_PARSE contains exactly
* one result field => build the result table
LOOP AT gt_parse INTO gw_parse.
* Strip quotes from text fields
REPLACE ALL OCCURRENCES OF '"' IN gw_parse WITH ''.
CASE sy-tabix.
WHEN 1. gw_result-company = gw_parse.
WHEN 2. gw_result-compnr = gw_parse.
WHEN 3. gw_result-city = gw_parse.
WHEN 4. gw_result-country = gw_parse.
ENDCASE.
ENDLOOP.
APPEND gw_result TO gt_result.
ENDLOOP.
* Show the formatted result
LOOP AT gt_result INTO gw_result.
WRITE: / gw_result-company, gw_result-compnr,
gw_result-city, gw_result-country.
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form reassemble
*&---------------------------------------------------------------------*
* Merges tokens that were split because they contain a comma
*----------------------------------------------------------------------*
FORM reassemble.
DATA: lastpos TYPE i,
lastchar TYPE c,
currtoken LIKE sy-tabix,
nexttoken LIKE sy-tabix,
gw_next LIKE gw_parse.
LOOP AT gt_parse INTO gw_parse.
lastpos = STRLEN( gw_parse ) - 1.
lastchar = gw_parse+lastpos(1).
* Token starts with quote but does not end with one =>
* must merge with the next token
IF gw_parse+0(1) = '"' AND lastchar <> '"'.
currtoken = sy-tabix.
nexttoken = sy-tabix + 1.
READ TABLE gt_parse INTO gw_next INDEX nexttoken.
CONCATENATE gw_parse gw_next INTO gw_parse SEPARATED BY ','.
MODIFY gt_parse FROM gw_parse INDEX currtoken.
DELETE gt_parse INDEX nexttoken.
ENDIF.
ENDLOOP.
ENDFORM. "reassemble
‎2009 Apr 08 9:40 AM
Hi,
you can try this way.. Try read the last value using the position and offset
DATA : l_len1 type i.
DATA : l_len2 type i.
LOOP AT gt_raw into gwa_raw.
IF SY-TABIX > 1.
SPLIT gwa_raw at ',' into gwa_cust-cust_code
gwa_cust-cust_name
gwa_cust-grp_name.
l_len1 = STRLEN( gwa_cust-cust_name ).
l_len2 = STRLEN( gwa_cust-cust_code ).
l_len1 = l_len1 + l_len2 + 2.
Clear gwa_cust-grp_name.
gwa_cust-grp_name = gwa_raw+len1.
APPEND gwa_cust TO gt_cust.
CLEAR: gwa_cust, gwa_raw.
ENDIF.
ENDLOOP.
‎2009 Apr 08 9:58 AM
hi avinash,
i dont get you exactly.
actually my coding is
LOOP AT gt_raw into gwa_raw.
IF SY-TABIX > 1.
SPLIT gwa_raw at ',' into gwa_cust-cust_code
gwa_cust-cust_name
gwa_cust-grp_name
gwa_cust-corp_name
gwa_cust-busi_name
gwa_cust-osc
gwa_cust-app_code
gwa_cust-app_desc
gwa_cust-local
gwa_cust-osc_code.
APPEND gwa_cust TO gt_cust.
CLEAR: gwa_cust, gwa_raw.
ENDIF.
ENDLOOP.
in this, i get ',' in all these gwa_cust-grp_name
gwa_cust-corp_name
gwa_cust-busi_name.
i actually need to retrieve the gwa_cust-osc_code for my further processing. But i am getting the correct value.
can u plz help me.
thanks.
‎2009 Apr 08 9:40 AM
You can capture it using two variables after using split.Then you can use concatenate statement.
‎2009 Apr 08 10:04 AM
Hi,In debugging mode, check the values that is getting read from the file. Split at ',' should work perfectly. Just check the data in the internal table gt_raw that you get after reading from the file and do the coding based on that. share with us the table data so that we can help you out on this.
‎2009 Apr 08 10:22 AM
the gt_raw data value is -
1234567,ABCELECTRONIC CO., LTD.,ABC ELECT,CHENG EHANDLE TECH. CO., LTD.,ABC ELECTRONIC CO., LTD.,F,1Z0,ELECTRONICS,0,CDVIKEN
actually it should split into
gwa_cust-cust_code = 1234567
gwa_cust-cust_name = ABC Electronic co., ltd.
gwa_cust-grp_name = ABC ELECT
gwa_cust-corp_name = CHENG EHANDLE TECH. CO., LTD.
gwa_cust-busi_name = ABC ELECTRONIC CO., LTD.
gwa_cust-osc = F
gwa_cust-app_code = 1Z0
gwa_cust-app_desc = ELECTRONICS
gwa_cust-local = 0
gwa_cust-osc_code = CDVIKEN.
but it is giving
gwa_cust-cust_code = 1234567
gwa_cust-cust_name = ABC Electronic co.
gwa_cust-grp_name = ltd.
gwa_cust-corp_name = ABC ELECT
gwa_cust-busi_name = CHENG EHANDLE TECH. CO.
gwa_cust-osc = LTD.
gwa_cust-app_code = ABC ELECTRONIC CO.
gwa_cust-app_desc = LTD.
gwa_cust-local = F
gwa_cust-osc_code = 1Z0
hope u can get it now.
since it is having comma inbetween the data. it is splitting with that comma itself .
thanks.
‎2009 Apr 08 10:40 AM
i think it is not possible..
as u r splitting the record at ','....
u have to change the seperator....
‎2009 Apr 08 10:30 AM
Hi,
When we use ',' as a delimiter, we have to see that data does not carry any comma's other then where they need to be delimited.
As far as i know, there is no option to distinguish between a comma used as a delimiter and a comma used in data.
Regards,
Chaitanya
‎2009 Apr 08 11:28 AM
Hi,
I notice you have marked the message as answered, but I just wanted to let you know there is a solution. The trick is to parse into an internal table and then to find and reassemble fields that were split because they contgain a comma. The ABAP program below is a commented example.
Rgds,
Mark
REPORT zcsv_parse.
DATA:
tokens TYPE i.
TYPES: BEGIN OF ty_result,
company TYPE char20,
compnr TYPE i,
city TYPE char30,
country TYPE char30,
END OF ty_result.
DATA:
gt_rawtab TYPE TABLE OF string,
gw_rawtab LIKE LINE OF gt_rawtab,
gt_result TYPE TABLE OF ty_result,
gw_result LIKE LINE OF gt_result,
gt_parse TYPE TABLE OF string,
gw_parse LIKE LINE OF gt_parse.
DEFINE %csvline.
gw_rawtab = &1.
append gw_rawtab to gt_rawtab.
END-OF-DEFINITION.
START-OF-SELECTION.
* Create CSV lines, some with a comma inside a token
%csvline '"CompanyOne NV",500,"Antwerp","Belgium"'.
%csvline '"CompanyTwo,Inc",600,"New York,NY","USA"'.
%csvline '"CompanyThree,Ltd",700,"Sydney,NSW","Australia"'.
* Parse the raw CSV
LOOP AT gt_rawtab INTO gw_rawtab.
REFRESH gt_parse.
SPLIT gw_rawtab AT ',' INTO TABLE gt_parse.
DESCRIBE TABLE gt_parse LINES tokens.
* If extra commas: token count higher than field count
IF tokens > 4.
PERFORM reassemble.
ENDIF.
* At this point each entry in GT_PARSE contains exactly
* one result field => build the result table
LOOP AT gt_parse INTO gw_parse.
* Strip quotes from text fields
REPLACE ALL OCCURRENCES OF '"' IN gw_parse WITH ''.
CASE sy-tabix.
WHEN 1. gw_result-company = gw_parse.
WHEN 2. gw_result-compnr = gw_parse.
WHEN 3. gw_result-city = gw_parse.
WHEN 4. gw_result-country = gw_parse.
ENDCASE.
ENDLOOP.
APPEND gw_result TO gt_result.
ENDLOOP.
* Show the formatted result
LOOP AT gt_result INTO gw_result.
WRITE: / gw_result-company, gw_result-compnr,
gw_result-city, gw_result-country.
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form reassemble
*&---------------------------------------------------------------------*
* Merges tokens that were split because they contain a comma
*----------------------------------------------------------------------*
FORM reassemble.
DATA: lastpos TYPE i,
lastchar TYPE c,
currtoken LIKE sy-tabix,
nexttoken LIKE sy-tabix,
gw_next LIKE gw_parse.
LOOP AT gt_parse INTO gw_parse.
lastpos = STRLEN( gw_parse ) - 1.
lastchar = gw_parse+lastpos(1).
* Token starts with quote but does not end with one =>
* must merge with the next token
IF gw_parse+0(1) = '"' AND lastchar <> '"'.
currtoken = sy-tabix.
nexttoken = sy-tabix + 1.
READ TABLE gt_parse INTO gw_next INDEX nexttoken.
CONCATENATE gw_parse gw_next INTO gw_parse SEPARATED BY ','.
MODIFY gt_parse FROM gw_parse INDEX currtoken.
DELETE gt_parse INDEX nexttoken.
ENDIF.
ENDLOOP.
ENDFORM. "reassemble
‎2009 Apr 09 9:13 AM
Thanks Mark.
It works absolutely fine. I thought there is no solution for this and told my func. cons. to give data without the , inbetween.
anyhow thanks once again.
‎2009 Apr 20 5:45 AM
Hi Mark !
Now again i have the problem in the csv file.
your code works if only one ',' comes between the "...".
but for me, now i get more then 3 commas between the csv file ( of one field ).
can u help me.
thanks