gpt4 book ai didi

python - 如何在xlsx中输入1行数据?

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:18 26 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序接受用户(我)的输入来创建客户可能想要订购的商品目录。到目前为止,一切都还好。但我好像有一个小问题。它不是为每个项目输入填充 1 行数据,而是填充 3 行。

我已经尝试删除

ws.write(row, col + 1, title)
ws.write(row, col + 2, cost, money)

但它只将 ISBN 输入写入 Excel 文件。

#data headers
ws.write('A2', 'ISBN', bold)
ws.write('B2', 'Title', bold)
ws.write('C2', 'Cost', bold)

#data to input into cells
isbn = input('ISBN: ')
title = input('Title: ')
cost = input('Cost: ')


#starting rows and columns
row = 2
col = 0

#write it out row by row
for details in (isbn, title, cost):
ws.write(row, col, isbn)
ws.write(row, col + 1, title)
ws.write(row, col + 2, cost, money)
row += 1

#total
ws.write(row, 1, 'Total', bold)
ws.write(row, 2, '=SUM(C:C) ')

wb.close()

预期:Excel 每个项目输入 1 行数据

实际结果:Excel 每个项目输入 3 行数据

最佳答案

问题出在您的 for 循环中。您正在循环查找列表中的三个项目中的每一个。如果您只想写入 3 个单独的项目一次,则可以删除循环。

或者,如果您想继续获取输入并通过将 input() 移动到循环中来写入它(但请确保添加某种退出循环的方法)。

关于python - 如何在xlsx中输入1行数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55584829/

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