Modifying a CSV File
Hi,
I need to modify the values of some fields of an incoming CSV file and output it in the same format with the modified values.
The file has a structure where the 1st record is the name of the field, the 2nd line is the actual values. There is the potential for other fields to be inserted in the file. So this incoming file is dynamic. But I know exactly which fields I need to modify by the technical name of the field in the 1st record.
BSEG-DMBTR, BSEG-WRBTR,BSEG-MWSTS
100.00;110.00;10.00
What is the best way to read this CSV file, Modify specific fields, and re-output it ?
Thanks!
There are many ways to skin a cat..... or whatever.
so are programming flavours.
1) for fixed record length, i.e., you know which exact column position contains a comma,
2) variable record length, i.e., you don't know where is the comma in the record, but you know how many are going to be coming in. This format will universally work. example....
Assuming you are reading sequential line by line from unix environment.
This example is very basic in nature which you can build on.
data: begin of record occurs 0,
fld1(5) type c,
fld2(5) type c,
fld3(5) type c,
end of record.
data: rec(512) type c.
start-of-selection.
perform open_file.
perform read_file.
form read_file.
data: first_rec_flg(1).
do.
clear rec.
read dataset infile into rec.
check sy-subrc = 0.
if first_rec_flg = k_true.
continue.
endif.
split rec at ',' into record-fld1
record-fld2
record-fld3
append record. clear record.
enddo.
Assuming you are reading from windows/presentation server.
You could use gui_upload to dump it in an internal table specifying "has_field_separator".
Note: all "description/text" is encapsulated in @"@ within a csv record and may require special handling based on your need.
Hope this helps.
Regards,
Maulik
Thanks for the ideas. The only problem is that I do not know in what order the fields I need will be in. I think I'll need to create a dynamic Internal Table. I'm on 45B so alot of the 46 functionality for this is not available. If you have any other ideas that would be great..
Thanks!
Thanks for the ideas. The only problem is that I do not know in what order the fields I need will be in. I think I'll need to create a dynamic Internal Table. I'm on 45B so alot of the 46 functionality for this is not available. If you have any other ideas that would be great..
Thanks!
You don't need to know the fields when you are dealing with a text file, especially a csv file, because everything is a text.
Since you would be getting the header line which tells you the order of the columns, you can create a field symbol and dynamically assign it to appropriate columns in your internal table. First build a generic record and read the data into it field by field, then based on your field symbol move it to another structure which you need.