Python:如何将.ofx文件转换为.csv

goucqfw6  于 2023-09-28  发布在  Python
关注(0)|答案(3)|浏览(120)

有谁知道如何将.ofx文件转换为.csv文件。使用Python语言?
我有一个文件的银行对账单有。ofx我想用它在我的个人软件只支持。csv格式。
请帮帮我

b5buobof

b5buobof1#

虽然有点晚,但是python包ofxparse可以打开和读取ofx文件。从那里,您可以查询您的交易并根据需要导出它们。

lawou6xi

lawou6xi2#

不是python,但是iCreateOFX Basic可以将OFX和QIF文件转换为CSV(反之亦然)。

pvabu6sv

pvabu6sv3#

from ofxparse import OfxParser
import csv

with open(r"data\Extrato31232179272.ofx") as fileobj:
    ofx = OfxParser.parse(fileobj)

lst_operacoes = []

for i in ofx.account.statement.transactions:
    lst_operacoes.append(
        {
            "payee": i.payee,
            "type": i.type,
            "date": i.date,
            "user_date": i.user_date,
            "amount": f"{i.amount}".replace(".", ","),
            "id": i.id,
            "memo": i.memo,
            "sic": i.sic,
            "mmc": i.mcc,
            "checksum": i.checknum,
        }
    )

colunas = [
    "payee",
    "type",
    "date",
    "user_date",
    "amount",
    "id",
    "memo",
    "sic",
    "mmc",
    "checksum",
]

# use latin-1 ou iso-8859-1 para windows
# use utf-8 para linux e mac
with open(r"data/extrato.csv", "w", encoding="latin-1", newline="") as filecsv:
    writer = csv.DictWriter(filecsv, fieldnames=colunas, dialect="excel")
    writer.writeheader()
    writer.writerows(lst_operacoes)

相关问题