解析交互式代理基础数据

时间:2018-03-13 06:08:53

标签: python

我使用api成功从IB中提取数据。它采用XML格式,看起来像这样......

 <TotalRevenues currency="USD">
      <TotalRevenue asofDate="2017-12-31" reportType="TTM" period="12M">239176000000.000000</TotalRevenue>
      <TotalRevenue asofDate="2017-09-30" reportType="TTM" period="12M">229234000000.000000</TotalRevenue>
      <TotalRevenue asofDate="2017-06-30" reportType="TTM" period="12M">223507000000.000000</TotalRevenue>
</TotalRevenues>
   <DividendPerShares currency="USD">
      <DividendPerShare asofDate="2017-12-31" reportType="A" period="3M">0.630000</DividendPerShare>
      <DividendPerShare asofDate="2017-09-30" reportType="A" period="3M">0.630000</DividendPerShare>
      <DividendPerShare asofDate="2017-09-30" reportType="A" period="12M">2.400000</DividendPerShare>
   </DividendPerShares>
   <Dividends currency="USD">
      <Dividend type="CD" exDate="2018-02-09" recordDate="2018-02-12" payDate="2018-02-15" declarationDate="2018-02-01">0.630000</Dividend>
      <Dividend type="CD" exDate="2017-11-10" recordDate="2017-11-13" payDate="2017-11-16" declarationDate="2017-11-03">0.630000</Dividend>
      <Dividend type="CD" exDate="2017-08-10" recordDate="2017-08-14" payDate="2017-08-17" declarationDate="2017-07-02">0.630000</Dividend>
   </Dividends>
   <EPSs currency="USD">
      <EPS asofDate="2017-12-31" reportType="A" period="3M">3.920000</EPS>
      <EPS asofDate="2017-09-30" reportType="A" period="3M">2.090000</EPS>
      <EPS asofDate="2017-09-30" reportType="A" period="12M">9.270000</EPS>
   </EPSs>
</FinancialSummary>

我想以这种格式将此信息转换为CSV格式:

            total revenue report type     period rev dividendpershare period div
2017-12-31  239176000000     ttm           12m        0.630000          3m
2017-09-30   229234000000    ttm           12m        0.630000          3m

有一种简单的方法吗?

1 个答案:

答案 0 :(得分:1)

You can try this:

import csv
import xmltodict

XML = """
<FinancialSummary>
    <TotalRevenues currency="USD">
        <TotalRevenue asofDate="2017-12-31" reportType="TTM" period="12M">239176000000.000000</TotalRevenue>
        <TotalRevenue asofDate="2017-09-30" reportType="TTM" period="12M">229234000000.000000</TotalRevenue>
        <TotalRevenue asofDate="2017-06-30" reportType="TTM" period="12M">223507000000.000000</TotalRevenue>
    </TotalRevenues>
    <DividendPerShares currency="USD">
        <DividendPerShare asofDate="2017-12-31" reportType="A" period="3M">0.630000</DividendPerShare>
        <DividendPerShare asofDate="2017-09-30" reportType="A" period="3M">0.630000</DividendPerShare>
        <DividendPerShare asofDate="2017-09-30" reportType="A" period="12M">2.400000</DividendPerShare>
    </DividendPerShares>
    <Dividends currency="USD">
        <Dividend type="CD" exDate="2018-02-09" recordDate="2018-02-12" payDate="2018-02-15" declarationDate="2018-02-01">0.630000</Dividend>
        <Dividend type="CD" exDate="2017-11-10" recordDate="2017-11-13" payDate="2017-11-16" declarationDate="2017-11-03">0.630000</Dividend>
        <Dividend type="CD" exDate="2017-08-10" recordDate="2017-08-14" payDate="2017-08-17" declarationDate="2017-07-02">0.630000</Dividend>
    </Dividends>
    <EPSs currency="USD">
        <EPS asofDate="2017-12-31" reportType="A" period="3M">3.920000</EPS>
        <EPS asofDate="2017-09-30" reportType="A" period="3M">2.090000</EPS>
        <EPS asofDate="2017-09-30" reportType="A" period="12M">9.270000</EPS>
    </EPSs>
</FinancialSummary>
"""


def write_to_csv(rows):
    header = 'date, total revenue, report type, period rev, dividendpershare, period div'.split(', ')
    with open('sample.csv', 'w', newline='') as fo:
        writer = csv.writer(fo)
        writer.writerow(header)
        writer.writerows(rows)


def main():
    d = xmltodict.parse(XML)
    root = d['FinancialSummary']
    total_revenue_list = root['TotalRevenues']['TotalRevenue']
    dividend_per_share_list = root['DividendPerShares']['DividendPerShare']
    rows = []
    for total_rev, dps in zip(total_revenue_list, dividend_per_share_list):
        row = [
            total_rev['@asofDate'],
            total_rev['#text'],
            total_rev['@reportType'],
            total_rev['@period'],
            dps['#text'],
            dps['@period']
        ]
        rows.append(row)

    write_to_csv(rows)


if __name__ == '__main__':
    main()

Which will produce a sample.csv like this:

date,total revenue,report type,period rev,dividendpershare,period div
2017-12-31,239176000000.000000,TTM,12M,0.630000,3M
2017-09-30,229234000000.000000,TTM,12M,0.630000,3M
2017-06-30,223507000000.000000,TTM,12M,2.400000,12M

This sample program is written in Python3, and used a third-party library named xmltodict, you can install it by pip install xmltodict.