[英]How to sum numbers with string from a text file when meet conditions in Python
我正在做一个库存系统,它要求我打印特定项目的分发列表。 如果特定商品已分发到同一商店超过 1 次,则需要将数量加在一起。
我不允许使用字典和 pandas。只能使用列表
项目需要用代码搜索
交易文本文件:
S001,T01,T恤,4
S001,T01,T恤,7
S002,T02,鞋子,9
S003,T02,鞋子,11
第一个是店铺代码,第二个是商品代码,后面是商品名称和配送数量
这是我的代码
x=open("transactions.txt","r")
numbers = []
total = 0
searchInput=input("Enter item code to search: ")
for line in x:
lines = line.strip()
s = lines.split(",")
numbers.append(s)
if not searchInput.lower() in lines.lower():
continue
column = -1
record = len(numbers)
for v in range(record):
if searchInput == s[1]:
column = v
if ((numbers[column][0] == numbers[column][0]) and (numbers[column][1] == numbers[column][1])):
num = int(numbers[column][-1])
total += num
if column >=0:
a=numbers[column][0]+","+numbers[column][1]+","+numbers[column][2]+","+str(total)
print(a)
当我为 searchInput 输入 T01 时,它可以正确打印最新的总数。 但是当我输入 T02 时,即使商店代码不同,它也会总结我的总数。 它应该打印 T02 的第 2 行而不加总。
当我为 searchInput 输入 T02 时,output 应该是这样的
S002,T02,鞋子,9
S003,T02,鞋子,11
我试图猜测你的具体用例,所以我的例子可能有点偏离。 首先,让我通过我的一些评论来完成您的一些代码:
numbers = []
total = 0
x = open("transactions.txt","r")
for line in x:
print(line)
lines = line.strip()
s = lines.split(",")
numbers.append(s)
print(numbers)
if not searchInput.lower() in lines.lower():
# keep only the lines which have a match with the search input
continue
column = -1
record = len(numbers)
print(f'len(numbers): {len(numbers)}')
for v in range(record):
#
if searchInput == s[2]:
# s is the current line, 2 denotes the 3rd entry e.g. the item
# column will be overwritten by v as long as searchInput matches s[2] exactly
# column will therefore be equal to len(numbers)-1
column = v
print(f'column: {column}')
if ((numbers[column][0] == numbers[column][0]) and (numbers[column][1] == numbers[column][1])):
# if one item equals itself, and another item equals itself, then:
# num is the integer of last value in the column record
num = int(numbers[column][-1])
total += num
请注意 output 是如何不清楚的。 使用 Python 的关键功能之一是让您的代码清晰易读 - 易于阅读,因此当其他人(或您未来的自己)返回此代码时,您可以从离开的地方继续。
根据您所说的,我尝试按以下方式改写您的代码。 请特别注意变量的命名。 请注意,此示例是一个非常基本的示例,如果您处理输入数据,您可能希望在其中包装一些 try-except 语句和错误处理。 当然,您也可以决定切换到其他库,例如 csv,因为您已经有了逗号分隔值,甚至pandas ,您可以在其中执行groupby ,命名列以便于阅读,甚至添加日期时间列,以便您可以过滤日期。 无论如何,我认为您要实现的目标如下:
searchInput = input("Enter item code to search: ").lower()
records = list()
transactions = open("transactions.txt","r")
for record in transactions:
# cleaning the record by stripping leading and trailing spaces, lowering cases and splitting on the commas
clean_record = record.strip().lower().split(',')
if any([searchInput in x for x in clean_record]):
# keep only the lines which have a match with the search input
records.append(clean_record)
total = dict()
for record in records:
# now that we have all the records we want
# we will retrieve the relevant information
# we want a the sum of the values, printed for each store
if record[0] not in total.keys():
total[record[0]] = int(record[-1])
else:
total[record[0]] += int(record[-1])
for shop, tot in total.items():
print(f'Shop: {shop} - Total: {tot}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.