07-06-2021 9:46 AM
I have a csv file which contains values separated by semi-colon(;) but there is also a string containing the semi-colon. I want to separate the values based on semi-colon except the one within quotes (i.e. the string).
Is there any function module of method in SAP ABAP using which the string containing delimiter is not separated but the rest of the values are?
07-06-2021 9:54 AM
You could do a simple split, and re-read all value & check if a value start with " it should end with " or you have to find the next row ending with a " (and not starting)
07-06-2021 10:29 AM
frdric.girod this can be done but I want a FM which can do this. Is there any FM already present which can do this?
07-06-2021 11:58 AM
1. FM is not the Good Way of Working, FM is old school and using FM is taking a risk for futur upgrade
2. There is not a Golden List of nice FM doing whatever you want
3. Code in Object !!!
07-06-2021 12:36 PM
Hi Vansika,
Instead of using the Function module, you can directly use the split statement in abap.
data : lv_module type string value 'sd;mm;fi;co'.
data : lv_tech1 type string,
lv_tech2 type string,
lv_tech3 type string,
lv_tech4 type string.
split lv_module at ';' INTO lv_tech1
lv_tech2
lv_tech3
lv_tech4.
write : lv_tech1,
/ lv_tech2,
/ lv_tech3,
/ lv_tech4.
07-06-2021 12:57 PM
That would be nice from you if you format your ABAP code with colors, indents and so on: that is automatically done by editing your question, selecting the lines and pressing the button CODE.
07-06-2021 1:42 PM
also, this probably doesn't handle the case above, with semi-colon appearing in quotes.
07-06-2021 12:48 PM
Yes. but this is a customer requirement.
Also, I found a FM after researching. Thanks for the help though 🙂
07-06-2021 1:22 PM
I could understand if it is a final customer requirement 😞
Could you post your FM for futur Customer strange question
07-06-2021 1:31 PM
Interesting. I would suggest to lets look at the problem before looking for a method or function. As you mentioned it is a CSV file that is using ; as a separator but also there is a string field in it that can also contain a ; in it (part of string text). so i assume the file structure could be like following
Field1;Field2;"StringFieldWith;and text"
In this case, you should split first at " (quote) so that you string field is separate from the other fields and then you can split at ; (semi colon). Like for the above structure we should have something like following code. Please note i just types it. may contain syntax errors but you should get the idea what i am suggesting.
* Suppose file structure is Field1;Field2;"StringFieldWith;and text"
DataRow = Field1;Field2;"StringFieldWith;and text"
Data: lv_buff1 type string, lv_buff2 type string.
Data begin of ls_file,
field1 type string,
field2 type string,
field3 type string,
end of ls_file.
split DataRow at '"' into lv_buff1 lv_buff2.
* Now string field should be in lv_buff2 and other fields in lv_buff1
* so you can split lv_buff1 at ; to get the rest of the fields
if sy-subrc = 0.
split lv_buf1 at ';' into ls_file-field1 ls_file-field2.
ls_file-field3 = lv_buff2.
endif.
<br>
07-06-2021 2:10 PM
Hi vg24,
do you have the possibility to save the excel file as text file tab delimited?
In this case, you can split the line by cl_abap_char_utilities=>HORIZONTAL_TAB
Best Regards
Thorsten
07-06-2021 6:24 PM
07-06-2021 10:18 PM
Please check this standard method cl_rsda_csv_converter=>csv_to_structure. This is for comma delimited. You can use the same logic.
DATA: lr_csv TYPE REF TO cl_rsda_csv_converter,
ls_file TYPE ty_read_file,
lv_string TYPE string.
CALL METHOD cl_rsda_csv_converter=>create
RECEIVING
r_r_conv = lr_csv.
DELETE ip_t_string INDEX 1.
LOOP AT ip_t_string INTO lv_string.
TRY.
lr_csv->csv_to_structure(
EXPORTING
i_data = lv_string
IMPORTING
e_s_data = ls_file ). --> Here you will get in your structure
Endloop.
07-07-2021 9:58 AM
Standard doesn't mean "released" (released = if there's a bug, SAP will support you and fix it if your point is valid).
07-07-2021 4:01 PM
09-09-2021 7:00 PM
Hi Giuseppi,
I just tested FM TEXT_CONVERT_CSV_TO_SAP. It doesn't respect the quotes. Can't help in this case.
Regards,
Nemanja