使用python解析复杂的csv数据?

时间:2018-04-19 15:41:36

标签: python csv

我有csv数据(csv),如:

id,247341,247341
company,    1 800 FLOWERS COM INC,  1 800 FLOWERS COM INC
ticker, FLWS,   FLWS
financial year end (month), 6,  6
industry code,  103002, 103002
indicator,  Share Price,    Common Shares Outstanding
2011-11-04, 2.72,   65046.232
2012-02-06, 2.89,   65065.558       
2012-05-04, 3.04,   64788.687       

读完这些数据后我不明白如何格式化输出数据?我想要这样的输出数据:

id:247341
company:1 800 FLOWERS COM INC
ticker: FLWS
financial year end (month):6
industry code:103002
indicator:{
                   #Based on date
    Share Price:2.72; Date:2011-11-04
    Share Price:2.89; Date:2012-02-06
    Common Shares Outstanding:65046.232;Date:2011-11-04
    Common Shares Outstanding:65065.558;Date:2012-02-06

}

1 个答案:

答案 0 :(得分:0)

使用您为CSV文件提供的文本,您可以使用以下方法:

import csv

def write_block(f_output, block):
    if block:

        f_output.write("""{}:{}
{}:{}
{}:{}
{}:{}
{}:{}
{}:{{
          #Based on date
""".format(
    block[0][0], block[0][1], 
    block[1][0], block[1][1], 
    block[2][0], block[2][1], 
    block[3][0], block[3][1], 
    block[4][0], block[4][1], 
    block[5][0]))

        for price in block[6:]:
            f_output.write('    {}:{};Date:{}\n'.format(block[5][1], price[1], price[0]))
        for price in block[6:]:
            f_output.write('    {}:{};Date:{}\n'.format(block[5][2], price[2], price[0]))

        f_output.write('\n}\n')

with open('input.csv', newline='') as f_input, open('output.txt', 'w') as f_output:
    csv_input = csv.reader(f_input, skipinitialspace=True)
    block = []

    for row in csv_input:
        if len(row):
            if row[0] == 'id':
                if block:
                    write_block(f_output, block)
                block = [row]
            else:
                block.append(row)

    write_block(f_output, block)

注意确保在复制脚本时保留缩进。

实际上,它使用CSV库读取文件中的每一行,直到找到另一条id行。然后将其视为block并写入输出文件。

我建议您添加print(block)以了解其工作原理。