gpt4 book ai didi

python - 有没有办法将我从 .txt 文件中检索到的整数相加

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:04 25 4
gpt4 key购买 nike

我尝试将它们从每一行添加起来,但它不起作用

import csv

FILE_NAME = "paintingJobs.txt" #I use this so that the file can be used easier
COL_HEADERS = ['Number', 'Date', 'ID', 'Total', 'Status', 'Paid']
NUM_COLS = len(COL_HEADERS)#This will insure that the header of each column fits into the length of the data

# read file once to determine maximum width of data in columns
with open(FILE_NAME) as f:
reader = csv.reader(f, delimiter=',')
# determine the maximum width of the data in each column
max_col_widths = [len(col_header) for col_header in COL_HEADERS]
for columns in reader:
for i, col in enumerate(columns):
if "A" in columns and int(columns[5]) < int(columns[3]):
max_col_widths[i] = max(max_col_widths[i], len(repr(col)))
# add 1 to each for commas
max_col_widths = [col_width+1 for col_width in max_col_widths]

# read file second time to display its contents with the headers
with open(FILE_NAME) as f:
reader = csv.reader(f, delimiter=',')
# display justified column headers
print(' ' + ' '.join(col_header.ljust(max_col_widths[i])
for i, col_header in enumerate(COL_HEADERS)))
# display justified column data
for columns in reader:
if "A" in columns and int(columns[5]) < int(columns[3]):
print(columns)
print ("Amount_Outstanding:"),
print (int(columns[3]) - int(columns[5])),

这是结果:

Number   Date          ID      Total   Status  Paid  
['E5345', '22/09/2015', 'C106', '815', 'A', '400']
Amount_Outstanding:
415
['E5348', '23/09/2015', 'C109', '370', 'A', '200']
Amount_Outstanding:
170
['E5349', '25/09/2015', 'C110', '480', 'A', '250']
Amount_Outstanding:
230
['E5353', '28/09/2015', 'C114', '272', 'A', '200']
Amount_Outstanding:
72
['E5355', '29/09/2015', 'C116', '530', 'A', '450']
Amount_Outstanding:
80
['E5363', '01/10/2015', 'C124', '930', 'A', '500']
Amount_Outstanding:
430
['E5364', '02/10/2015', 'C125', '915', 'A', '800']
Amount_Outstanding:
115
['E5367', '03/10/2015', 'C128', '427', 'A', '350']
Amount_Outstanding:
77
['E5373', '10/10/2015', 'C134', '1023', 'A', '550']
Amount_Outstanding:
473

所以我想知道是否可以将每个 Amount_Outstanding 的总数相加并将其更改为不同的变量,请记住我是 python 和 csv 的新手

最佳答案

您可以通过创建一个“outstanding_amount”列表来解决这个问题,您可以将所有未清金额放入其中并获胜:D

#build the list
outstanding_amount = []

# read file second time to display its contents with the headers
with open(FILE_NAME) as f:
reader = csv.reader(f, delimiter=',')
# display justified column headers
print(' ' + ' '.join(col_header.ljust(max_col_widths[i])
for i, col_header in enumerate(COL_HEADERS)))
# display justified column data
for columns in reader:
if "A" in columns and int(columns[5]) < int(columns[3]):
print(columns)
print ("Amount_Outstanding:")
print (int(columns[3]) - int(columns[5]))

###### NEW PART ######
outstanding_amount.append(int(columns[3]) - int(columns[5]))

# Show the sum of all collected outstanding amount
print (sum(outstanding_amount))

您甚至可以通过执行类似的操作来访问数据集。

例如,如果您想知道第二项的未结金额。然后你就可以:

print outstanding_amount[1] # Since we start from 0, the second item is at index 1

关于python - 有没有办法将我从 .txt 文件中检索到的整数相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611899/

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