`

Upload a Comma Delimited CSV file that contains commas in data

    博客分类:
  • ABAP
 
阅读更多
source:http://wiki.sdn.sap.com/wiki/display/Snippets/Upload+a+Comma+Delimited+CSV+file+that+contains+commas+in+data

Some Comma Delimited CSV files will contain commas in the data fields and this causes problems in loading the file into our system. I don't think there is a standard function module to do this but I will share a small code snippet which will save someone to re-invent the wheel.

Here is a sample of the comma delimited CSV text file, you can copy below text then paste in notepad and save as c:\Temp\Book1.csv file.

10,Shafiq,"Sacramento, CA"
11,Willie,"New York, NY"
12,Conner,"Seattle, WA"

Here is the sample program which will read this file and store in a table that will preserve the commas in the city field:
Upload from Local PC:
TYPES: BEGIN OF kcde_intern_struc.
        INCLUDE STRUCTURE  kcde_cells.
TYPES: END OF kcde_intern_struc.


DATA: l_intern    TYPE  TABLE OF kcde_intern_struc WITH HEADER LINE.

DATA: BEGIN OF it_output OCCURS 0,
        id(10) TYPE c,
        name(20) TYPE c,
        location(20) TYPE c,
  END OF it_output.
DATA: wa_output LIKE LINE OF it_output.
DATA: v_index TYPE i.

FIELD-SYMBOLS : <fs>.


CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
  EXPORTING
    i_filename      = 'C:\Temp\Book1.csv'
    i_separator     = ','
  TABLES
    e_intern        = l_intern
  EXCEPTIONS
    upload_csv      = 1
    upload_filetype = 2.

LOOP AT l_intern.
  MOVE : l_intern-col TO v_index.
  ASSIGN COMPONENT v_index OF STRUCTURE wa_output TO <fs>.
  MOVE : l_intern-value TO <fs>.
  AT END OF row.
    APPEND wa_output TO it_output.
    CLEAR wa_output.
  ENDAT.
ENDLOOP.

Upload from Unix Server
DATA: BEGIN OF itab_upload OCCURS 0,
       str(4096),
      END OF itab_upload.
DATA: l_intern    TYPE  TABLE OF kcde_intern_struc WITH HEADER LINE.

  OPEN DATASET p_path FOR INPUT ENCODING UTF-8 IN TEXT MODE.
  DO.
    READ DATASET p_path INTO itab_upload.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    APPEND itab_upload.
  ENDDO.
  CLOSE DATASET p_path.
  PERFORM separated_to_intern_convert IN PROGRAM saplkcde
                                  TABLES itab_upload l_intern USING ','.
  LOOP AT l_intern.
    MOVE : l_intern-col TO v_index.
    ASSIGN COMPONENT v_index OF STRUCTURE wa_output TO <fs>.
    MOVE : l_intern-value TO <fs>.
    AT END OF row.
      APPEND wa_output TO it_output.
      CLEAR wa_output.
    ENDAT.
  ENDLOOP.
分享到:
评论

相关推荐

    HR_comma_sep.csv

    通过Kaggle的HR数据集分析身边的小伙伴们为什么要离职,并基于数据集中的特征制定一个评分卡模型来预测会不会离职。 数据集解释 总览:该HR数据集收集了15000份数据,其中3571人离职,离职率23.8%。...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    From there, the examples mirror the sqlite3 examples, including creating a database and table, loading data in a CSV input file into a database table, updat‐ ing records in a table using a CSV input...

    人力资源分析项目数据集HR_comma_sep.csv

    人力资源分析项目数据集HR_comma_sep.csv

    ag_news_csv.tgz

    The file classes.txt contains a list of classes corresponding to each label. The files train.csv and test.csv contain all the training samples as comma-sparated values. There are 3 columns in them, ...

    weka使用指导

    comma-separated value (CSV) format as a list of records with commas b‹ items. Having done this, you need only load the file into a text editor o processor; add the dataset’s name using the 4...

    csv jdbc 封装连接csv JDBC driver for CSV files

    用jdbc 连接并读取 csv JDBC driver for CSV ...CsvJdbc is a simple read-only JDBC driver that uses Comma Separated Value (CSV) files as database tables. It is ideal for writing data importing programs.

    OutlookAttachView v2.73

    properly if the folder name contains a comma character. * Version 1.81 o Fixed a crash problem when trying to save outlook attachment. * Version 1.80 o Added 'Add' buttons, which allows you to ...

    java操作csv文件(读、写)

    在做项目的时候,发现使用POI无法解析以csv文件结尾的文件,虽然csv文件能用Excel打开,但是csv文件没有像Excel一样有规定的电子表格形式,故使用POI无法解析csv文件,在网上找了一下,发现java有提供java csv文件来...

    csvwrite_with_headers.zip_csv to .mat_csvwrite_open

    covert output matlab file .mat to comma seaparated file csv so one can open in microsoft excel

    端口查看工具

    o Fixed bug in saving as comma-delimited file when field values contained comma character. * Version 1.34: o New Option: Remember Last Filter (The filter is saved in cports_filter.txt) * Version...

    rainbow_csv:彩虹CSV-Vim插件:突出显示CSV和TSV文件中的列,并以类似SQL的语言运行查询

    Vundle: Plugin 'mechatroner/rainbow_csv' VimPlug: Plug 'mechatroner/rainbow_csv' dein: call dein#add('mechatroner/rainbow_csv') 无需其他步骤-Rainbow CSV可以直接使用。 总览 主要特点: 用不同的...

    python读取csv文件指定行的2种方法详解

    csv是Comma-Separated Values的缩写,是用文本文件形式储存的表格数据,比如如下的表格 就可以存储为csv文件,文件内容是: No.,Name,Age,Score 1,Apple,12,98 2,Ben,13,97 3,Celia,14,96 4,Dave,15,95 假设上述...

    cdma matlab仿真

    simcdma.m allows a set of CDMA parameters to be simulated, such as BER verse number of users, etc. All the graphs used in ...is a Result.txt file which is a comma delimited spread sheet compatible file.

    人力资源数据HR_comma_sep

    人力资源数据包含15000行数据信息,可以用于预处理、数据可视化分析以及数据挖掘分析,请勿商业,探讨学习使用

    ag_news文本分类数据集

    The file classes.txt contains a list of classes corresponding to each label. The files train.csv and test.csv contain all the training samples as comma-sparated values. There are 3 columns in them, ...

    cdma.rar_The Number_cdma ber_cdma users

    simcdma.m allows a set of CDMA parameters to be ...is a Result.txt file which is a comma delimited spread sheet compatible file. simcdma.m also has all the settings for the CDMA link parameters.

    csv文件合并.bat

    CSV全称Comma-Separated Values,它是一种通用的,简单的,被广泛采用的一种表格数据格式。采用纯文本格式存储,用分隔符号分开。它格式类似于数据库表格,每一行,中间用分隔符分开,一行就是一条记录,一列就是一...

    Unity插件之CSV

    unity开发使用的csv简易插件,逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列...

    Address Book with Multiple Field Search(个人信息管理系统)

    For convenience, assume that none of the fields have spaces or commas in them, so you can read a comma separated file without worrying about those (non-algorithm related) complications. No need to ...

Global site tag (gtag.js) - Google Analytics