附加了奇怪的csv头

时间:2014-05-08 16:33:51

标签: python csv

运行此代码时,将列表的第一个元素附加到标题的末尾。 我可能做错了什么? 谢谢

DETAIL_CODE,ZIP_CODE,UPD_KEY_NUM,ACTION_CODE,RECORD_TYPE,CARRIERR_ID,ST_PDIR_ABBRV,ST_NAME,ST_SUF_ABBRV,ST_POSTD_ABBRV,AD_P_LNUMB,AD_P_HNUMB,AD_P_PARITY,BUILD_FIRM_NM,AD_S_ABBRV,AD_S_LNUM,AD_S_HNUM,AD_S_PARITY,PLS4_LN_ZSNUM,PLS4_LN_ZSEGNUM,PLS4_HN_ZSNUM,PLS4_HN_ZSEGNUM,BASE_ALT_CODE,LACS_STA_IND,GOV_BUILD_IND,FINANCE_NUM,STATE_ABBRV,COUNTY_NUM,CONGR_DIST_NUM,MUNIC_CT_STATE_KEY,URB_CT_STATE_KEY,PREF_LINE_STATE_KEYC, ,COPYRIGHT(,C,), 05-,14, USPS 001                   ,    ,  ,          ,          , ,                                        ,    ,        ,  , ,  ,  ,  ,  , , , ,      ,  ,   ,  ,      ,      ,

import csv

c = [['C', '     ', 'COPYRIGHT(', 'C', ')', ' 05-', '14', ' USPS 001                   ', '    ', '  ', '          ', '          ', ' ', '                                        ', '    ', '        ', '        ', ' ', '  ', '  ', '  ', '  ', ' ', ' ', ' ', '      ', '  ', '   ', '  ', '      ', '      ', '     '],
    ['D', '80001', 'Z113111126', 'A', 'P', 'B001', '  ', 'PO BOX                      ', '    ', '  ', '0000000001', '0000000015', 'B', '                                        ', '    ', '        ', '        ', ' ', '00', '01', '00', '15', 'B', ' ', ' ', '070414', 'CO', '059', '07', '      ', '      ', 'Z1104'],
    ['D', '80001', 'Z113111127', 'A', 'P', 'B003', '  ', 'PO BOX                      ', '    ', '  ', '3A        ', '3A        ', 'B', '                                        ', '    ', '        ', '        ', ' ', '00', '20', '00', '20', 'B', ' ', ' ', '070414', 'CO', '059', '07', '      ', '      ', 'Z1104'],
    ['D', '80001', 'Z113111128', 'A', 'P', 'B003', '  ', 'PO BOX                      ', '    ', '  ', '3B        ', '3B        ', 'B', '                                        ', '    ', '        ', '        ', ' ', '00', '30', '00', '30', 'B', ' ', ' ', '070414', 'CO', '059', '07', '      ', '      ', 'Z1104']]

f = open('./out/test_write_csv.csv', 'wt')
f.write('DETAIL_CODE,ZIP_CODE,UPD_KEY_NUM,ACTION_CODE,RECORD_TYPE,CARRIERR_ID,ST_PDIR_ABBRV,ST_NAME,ST_SUF_ABBRV,ST_POSTD_ABBRV,AD_P_LNUMB,AD_P_HNUMB,AD_P_PARITY,BUILD_FIRM_NM,AD_S_ABBRV,AD_S_LNUM,AD_S_HNUM,AD_S_PARITY,PLS4_LN_ZSNUM,PLS4_LN_ZSEGNUM,PLS4_HN_ZSNUM,PLS4_HN_ZSEGNUM,BASE_ALT_CODE,LACS_STA_IND,GOV_BUILD_IND,FINANCE_NUM,STATE_ABBRV,COUNTY_NUM,CONGR_DIST_NUM,MUNIC_CT_STATE_KEY,URB_CT_STATE_KEY,PREF_LINE_STATE_KEY')

try:
    writer = csv.writer(f)
    for i in c:
        writer.writerow(i)
finally:
    f.close()

2 个答案:

答案 0 :(得分:4)

你忘了在最后写一个换行符。

但是,请将其留给csv模块;将标题写成一行:

writer.writerow(['DETAIL_CODE', 'ZIP_CODE', 'UPD_KEY_NUM', 'ACTION_CODE',
                 'RECORD_TYPE', 'CARRIERR_ID', 'ST_PDIR_ABBRV', 'ST_NAME', 
                 'ST_SUF_ABBRV', 'ST_POSTD_ABBRV', 'AD_P_LNUMB', 'AD_P_HNUMB',
                 'AD_P_PARITY', 'BUILD_FIRM_NM', 'AD_S_ABBRV', 'AD_S_LNUM',
                 'AD_S_HNUM', 'AD_S_PARITY', 'PLS4_LN_ZSNUM', 'PLS4_LN_ZSEGNUM',
                 'PLS4_HN_ZSNUM', 'PLS4_HN_ZSEGNUM', 'BASE_ALT_CODE', 'LACS_STA_IND',
                 'GOV_BUILD_IND', 'FINANCE_NUM', 'STATE_ABBRV', 'COUNTY_NUM', 
                 'CONGR_DIST_NUM', 'MUNIC_CT_STATE_KEY', 'URB_CT_STATE_KEY',
                 'PREF_LINE_STATE_KEY'])

因为csv模块通常会为这些文件写a \r\n line separator,具体取决于方言。

答案 1 :(得分:1)

标题后面需要换行符(\ n):

f.write('DETAIL_CODE, ... PREF_LINE_STATE_KEY\n')