如何迭代目录中文本文件的内容,并在python中进行特定的计算?
在这里,listOfLines将以列表项的形式显示内容,但我不知道下一步要做什么?试试这个。代码注释中的解释:如何迭代目录中文本文件的内容,并在python中进行特定的计算?,python,Python,在这里,listOfLines将以列表项的形式显示内容,但我不知道下一步要做什么?试试这个。代码注释中的解释: path=input("Enter the file name:") listOfLines = list() with open (path, "r") as myfile: for line in myfile: listOfLines.append(line.strip()) 输出: path=input("Enter the file name:")
path=input("Enter the file name:")
listOfLines = list()
with open (path, "r") as myfile:
for line in myfile:
listOfLines.append(line.strip())
输出:
path=input("Enter the file name:")
# declare variables
lst_item = []
lst_price = []
discount = 0
# open file and load lines into a list
# strip() all items in the list to remove new lines and extra spaces
with open (path, "r") as myfile:
lines = [line.strip() for line in myfile.readlines()]
# loop through the lines
for line in lines:
# if the line is not empty and doesn't include blank line
if line and "blank line in b/w" not in line:
# split the line by SPACE " "
split_line = line.split(" ")
# join all the items of the list except the last one
# to convert a word like Ice Cream back to one string
# set them as the item
item = " ".join(split_line[:-1])
# set the price as the last item of the list
price = split_line[-1]
# if the price is free set it to 0
if price == "Free":
price = 0
# If Discount is in the item string, set the discount variable
if "Discount" in item:
discount = int(price)
# Else add item and price to lst_item and lst_price
else:
lst_item.append(item)
lst_price.append(int(price))
print (item, price)
# sum all prices in lst price
subtotal = sum(lst_price)
# minus discount
after_discount = subtotal - discount
total = after_discount
print ("Subtotal:", subtotal, "Disount:", discount, "Total:", total)
但我不知道下一步该怎么办?这不是我们真正可以帮助的事情。请看。另一方面,变量和函数名应遵循带有下划线的小写形式,除非有充分理由不这样做。
path=input("Enter the file name:")
# declare variables
lst_item = []
lst_price = []
discount = 0
# open file and load lines into a list
# strip() all items in the list to remove new lines and extra spaces
with open (path, "r") as myfile:
lines = [line.strip() for line in myfile.readlines()]
# loop through the lines
for line in lines:
# if the line is not empty and doesn't include blank line
if line and "blank line in b/w" not in line:
# split the line by SPACE " "
split_line = line.split(" ")
# join all the items of the list except the last one
# to convert a word like Ice Cream back to one string
# set them as the item
item = " ".join(split_line[:-1])
# set the price as the last item of the list
price = split_line[-1]
# if the price is free set it to 0
if price == "Free":
price = 0
# If Discount is in the item string, set the discount variable
if "Discount" in item:
discount = int(price)
# Else add item and price to lst_item and lst_price
else:
lst_item.append(item)
lst_price.append(int(price))
print (item, price)
# sum all prices in lst price
subtotal = sum(lst_price)
# minus discount
after_discount = subtotal - discount
total = after_discount
print ("Subtotal:", subtotal, "Disount:", discount, "Total:", total)
Enter the file name:in.txt
Chocolate 50
Biscuit 35
Ice cream 50
Rice 100
Chicken 250
Perfume 0
Soup 0
Discount 80
Subtotal: 485 Disount: 80 Total: 405