gpt4 book ai didi

javascript - 重组 CSV 文件?

转载 作者:行者123 更新时间:2023-11-28 08:42:38 27 4
gpt4 key购买 nike

我有基本的脚本知识,但我不知道如何解决这个问题。我正在尝试将银行自动生成的 CSV 文件转换为 YNAB(您需要预算)可以理解的格式。

YNAB 格式(所需的 csv 文件格式)

Date,Payee,Category,Memo,Outflow,Inflow
07/25/10,Sample Payee,,Sample Memo for an outflow,100.00,
07/26/10,Sample Payee 2,,Sample memo for an inflow,,500.00

银行格式(csv 文件的当前状态)

"Account Type","Account Number","Transaction Date","Cheque Number","Description 1","Description 2","CAD$","USD$"
Chequing,00401-1234567,8/19/2013,,"Transfer","WWW TRANSFER - 0645 ",50.00,,
Chequing,00401-1234567,8/19/2013,,"STARBUCKS AC. U","IDP PURCHASE - 6678 ",-1.94,,

(所有虚假数据)

如何将当前的 CSV 文件转换为所需的格式?

最佳答案

这应该做:

import csv
import sys

if sys.hexversion >= 0x3000000:
inp = input
else:
inp = raw_input

def main():
input_file = inp('Name of bank statement file? ')
output_file = inp('Save YNAB output file as? ')

with open(input_file, 'rb') as inf, open(output_file, 'wb') as outf:
incsv = csv.reader(inf)
outcsv = csv.write(outf)
# deal with headers
header = next(incsv)
outcsv.write(['Date', 'Payee', 'Category', 'Memo', 'Outflow', 'Inflow'])
# translate data
for row in incsv:
cad = float(row[6])
ynab = [row[2], row[4], '', row[5], -cad if cad < 0. else '', cad if cad > 0. else '']
outcsv.writerow(ynab)

if __name__=="__main__":
main()

关于javascript - 重组 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20317091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com