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 in background

Former Member
0 Likes
1,211

I use the open dataset for uploading CSV file in background but I get the values with "' and , characters.

What am I doing wrong? Can I get an example ?

Thanks,

Rebeka

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
783

Hi,

If I remember correctly, when using OPEN DATASET, the file name can not have spaces in the name. Try renaming your file removing spaces and retry.

For your code, you'll need to breakdown the CSV file by the delimiter.

This code makes an archive file while reading the main file.

OPEN DATASET filname IN TEXT MODE MESSAGE t_mesg.

IF sy-subrc NE 0.

MOVE 'X' TO t_error.

MESSAGE e100(z0) WITH 'Error reading file:' t_mesg.

EXIT.

ENDIF.

IF p_load = 'X'.

OPEN DATASET archfilname FOR OUTPUT IN TEXT MODE MESSAGE t_mesg.

IF sy-subrc NE 0.

MOVE 'X' TO t_error.

MESSAGE e100(z0) WITH 'Error opening achrive file:' t_mesg.

EXIT.

ENDIF.

ENDIF.

DO.

READ DATASET filname INTO my_rec.

IF sy-subrc NE 0.

EXIT.

ENDIF.

IF p_load = 'X'.

TRANSFER my_rec TO archfilname.

ENDIF.

SPLIT my_rec AT c_tab " Here my delimter was a tab change to ',' for comma

INTO in_rec-id

in_rec-fname

in_rec-lname

in_rec-addr

in_rec-apt

in_rec-city

in_rec-state

in_rec-zip

in_rec-branch

in_rec-phone1

in_rec-phone2

in_rec-phone3

in_rec-phone3_ext

in_rec-email

in_rec-hear

in_rec-prefcont

in_rec-ownland

in_rec-build

in_rec-ownhome

  • in_rec-get_promo

in_rec-cmt1

in_rec-subdate.

APPEND in_rec TO it_input.

ENDDO.

CLOSE DATASET filname.

IF p_load = 'X'.

CLOSE DATASET archfilname.

DELETE DATASET filname.

ENDIF.

ENDIF.

Refer This:

I use the open dataset for uploading CSV file in background but I get the values with "' and , characters.

What am I doing wrong? Can I get an example ?

Thanks,

Rebeka

3 REPLIES 3
Read only

Former Member
0 Likes
783

Hi Rebeka,

While reading CSV file you have to SPLIT at , to get the values of each fields separately.

Regards,

Immanuel.

Read only

Former Member
0 Likes
783

Hi Rebeka,

In the CSV file if a single field value contains ,(comma) as a character, then the CSV format puts " at the begining and at the end. You need to handle this by putting some logic. meand at the time of spliting the values u need to consider " for making it understand it is single field value.

Reagrds,

Amitava

Read only

Former Member
0 Likes
784

Hi,

If I remember correctly, when using OPEN DATASET, the file name can not have spaces in the name. Try renaming your file removing spaces and retry.

For your code, you'll need to breakdown the CSV file by the delimiter.

This code makes an archive file while reading the main file.

OPEN DATASET filname IN TEXT MODE MESSAGE t_mesg.

IF sy-subrc NE 0.

MOVE 'X' TO t_error.

MESSAGE e100(z0) WITH 'Error reading file:' t_mesg.

EXIT.

ENDIF.

IF p_load = 'X'.

OPEN DATASET archfilname FOR OUTPUT IN TEXT MODE MESSAGE t_mesg.

IF sy-subrc NE 0.

MOVE 'X' TO t_error.

MESSAGE e100(z0) WITH 'Error opening achrive file:' t_mesg.

EXIT.

ENDIF.

ENDIF.

DO.

READ DATASET filname INTO my_rec.

IF sy-subrc NE 0.

EXIT.

ENDIF.

IF p_load = 'X'.

TRANSFER my_rec TO archfilname.

ENDIF.

SPLIT my_rec AT c_tab " Here my delimter was a tab change to ',' for comma

INTO in_rec-id

in_rec-fname

in_rec-lname

in_rec-addr

in_rec-apt

in_rec-city

in_rec-state

in_rec-zip

in_rec-branch

in_rec-phone1

in_rec-phone2

in_rec-phone3

in_rec-phone3_ext

in_rec-email

in_rec-hear

in_rec-prefcont

in_rec-ownland

in_rec-build

in_rec-ownhome

  • in_rec-get_promo

in_rec-cmt1

in_rec-subdate.

APPEND in_rec TO it_input.

ENDDO.

CLOSE DATASET filname.

IF p_load = 'X'.

CLOSE DATASET archfilname.

DELETE DATASET filname.

ENDIF.

ENDIF.

Refer This: