csv2ofx,解析器必须是字符串或字符流,而不是非类型

2024-10-06 20:27:16 发布

您现在位置:Python中文网/ 问答频道 /正文

谢谢大家的关注! 我的问题陈述是:使用开源库将.csv文件转换为.ofx文件。 我建议的解决方案是:使用csv2ofx python库将.csv文件转换为.ofx

csv文件的描述:My.csv文件包含以下列:

  • 交收日期
  • 交易日期
  • 符号
  • 名字
  • 交易类型
  • 数量
  • 价格
  • 佣金和费用
  • 数量

映射:我应用自定义映射(因为我有不同的列),如下所示:

from operator import itemgetter
mapping = {
    'has_header': True,
    'is_split': False,
    'delimiter': ',',
    'amount': itemgetter('Amount'),
    'date': itemgetter('Settlement date'),
    'symbol':itemgetter('Symbol'),
    'type': itemgetter('Transaction type'),
    'payee': itemgetter('Name'),
    'notes': itemgetter('Commissions and fees'),
}

代码:我编写以下代码将.csv转换为.ofx

ofx = OFX(mapping)
records = read_csv('Cleaned1.csv')
groups = ofx.gen_groups(records)
trxns = ofx.gen_trxns(groups)
cleaned_trxns = ofx.clean_trxns(trxns)
data = utils.gen_data(cleaned_trxns)
content = it.chain([ofx.header(), ofx.gen_body(data), ofx.footer()])

路障:当我运行代码时,它抛出以下错误:

TypeError Traceback (most recent call last)
<ipython-input-389-0d57618f3bd8> in <module>
      1 with open('Cleaned_new.ofx', 'wb') as f:
----> 2     for line in IterStringIO(content):
      3         print(line)
      4         f.write(line)

TypeError: Parser must be a string or character stream, not NoneType

我真的很感谢你抽出时间来阅读我的问题,期待听到你的建议或解决方案。多谢各位


Tags: 文件csv代码data数量line解决方案mapping