gpt4 book ai didi

python - 在使用 python 脚本导入 postgres 之前,如何在 csv 文件中划分单元格?

转载 作者:行者123 更新时间:2023-11-29 13:58:50 25 4
gpt4 key购买 nike

我想通过将销售美元/销售单位(如果销售单位为 0,则为 0)插入到 postgres sql 中来插入产品价格。插入数据库时​​我可以这样做还是必须运行更新? (见下文)

arg = {
'sales_dollars': row[8].strip()
'sales_units': row[9].strip()
'average_on_hand_dollars': row[11].strip()
'average_on_hand': row[12].strip() }

cur.execute(
"""INSERT INTO
"Sales_Inventory"("sales_dollars","sales_units", "price", "average_on_hand_dollars","average_on_hand", "cost")
select
%(sales_dollars)s,
%(sales_units)s,
#price = sales_dolars/sales_unit should go here
%(average_on_hand_dollars)s,
%(average_on_hand)s
#cost = average_on_hand_dollars/average_on_hand should go here
;""", arg)

提前致谢

最佳答案

只需添加另一个键值对:

arg['price'] = arg['sales_dollars']/arg['sales_units']

cur.execute( # ...
%(price)s
# ...

当然,您的值是字符串,而不是数字,因此 / 不会对您有多大帮助。但这很容易修复:转换为数字、除法,然后可选择转换回字符串:

arg['price'] = float(arg['sales_dollars'])/float(arg['sales_units'])

关于python - 在使用 python 脚本导入 postgres 之前,如何在 csv 文件中划分单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25455799/

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