使用制表符分隔文件

时间:2017-10-19 15:42:03

标签: sql oracle external-tables

我有一个包含制表符分隔数据的文件。类似于以下内容

id    f_name    l_name    m_name    dob        add_date  

100   john      smith     r         19880212   20161010  

102   micheal   ford                19920707   20161010  

我使用以下脚本通过external table

加载数据
CREATE TABLE student_info(ID NUMBER, F_NAME VARCHAR2(200 BYTE),
    L_NAME VARCHAR2(200 BYTE),
    M_NAME VARCHAR2(200 BYTE),
    DOB DATE, ADD_DATE DATE) 
ORGANIZATION EXTERNAL(TYPE ORACLE_LOADER 
                      DEFAULT DIRECTORY DATA_FILE_DIR 
                      ACCESS PARAMETERS(RECORDS DELIMITED BY NEWLINE 
                                   BADFILE bad_file_dir: 'STUDENT_COMP_INFO.bad'
                                   LOGFILE log_file_dir: 'STUDENT_COMP_INFO.log'
                                   SKIP 1 FIELDS TERMINATED BY 0X '09'
                                     OPTIONALLY ENCLOSED BY '"'
    LRTRIM MISSING FIELD VALUES ARE NULL) LOCATION(FILE_DIR: 'STUDENT_COMP_INFO.txt')) REJECT
LIMIT
UNLIMITED;

数据在数据中成功加载,但是当我从表中选择数据时,我得到以下输出。

SELECT * FROM STUDENT_INFO  

'ID'   'F_NAME'   'L_NAME'   'M_NAME'     'DOB'       'ADD_DATE'  

'100'  'john'     'smith'    'r'          '19880212'  '20161010'  

'102'  'micheal'  'ford'     '19920707'   '20161010'    

所以基本上它不会考虑Null值。如果Null def get_thing_dict(self): results = {} grand_total = 0.0 # String looks like "a1,b,c1,d1,a2,b2,c2,d2" etc. new_string = self.get_string().split(',') while new_string[:4]: # Get group of 4 new_string_line = new_string[:4] # Split data location = new_string_line[0] description = new_string_line[1] price = new_string_line[2] qty = new_string_line[3] # Add parts to dictionary results[location] = {'description': description, 'price': price, 'qty': qty} # Calculate and update total line_total = float(price) * int(qty) grand_total += line_total # Remove used data new_string = new_string[4:] # Add total to dictionary results['total'] = grand_total return results ,我怎样才能在每个字段中正确加载值?提前致谢。

1 个答案:

答案 0 :(得分:0)

我经历过同样的行为。 (解决方法)解决方案是删除

OPTIONALLY ENCLOSED BY '"'

对我来说似乎是一个错误。至少有一种解决方法,您可以在第二步中删除封闭字符。