如何从文本文件中读取python中的下一行
我有一个类似于此类的文本文件,该文件是从服务器读取的:
Jo's Deli Express
Potatoes
30
Fish
20
Chicken Meat
15
Margo's Grocery
Beans
20
Carrots
15
Apples
30
两家商店被一个空行分开,以便我们可以计算每个商店的值。
我想计算这些商店的产品的总收入(单个商品的产品 *价值的数字)以及总共购买的商品数量。
我遇到的问题是我不知道如何将项目名称链接到下面的值。因此,python需要知道,当它正在阅读每一行时,应该知道将什么值乘以下面的数字。例如,一块马铃薯的成本为0.70,因此它应该将0.7乘以30,依此类推,依此类推。
这是我到目前为止编码的内容:
user = input("Enter file name - AprilSales, MaySales, or JuneSales")
revenue = 0
response = None
try:
response = urllib.request.urlopen("link of server" + user + ".txt")
except:
print("File not found")
fileData = response.read().splitlines()
for i in range(len(fileData)):
fileData[i] = fileData[i].decode('utf-8')
for line in fileData:
if len(line) != 0:
#try block to only convert the strings which are able to be converted to ints and leave the rest
try:
print(int(line))
except:
print(line)
if(line == "Potatoes"):
revenue = revenue + int(line.next() * 0.7)
if(line == "Fish"):
revenue = revenue + int(line.next() * 1.50)
if(line == "Chicken"):
revenue = revenue + int(line.next() * 1.70)
else:
break
I have a text file that resembles like this which is read from a server:
Jo's Deli Express
Potatoes
30
Fish
20
Chicken Meat
15
Margo's Grocery
Beans
20
Carrots
15
Apples
30
The two shops are separated by an empty line so that we can calculate values for each store.
I want to calculate the total revenue of products(the numbers beneath the product * value for a single item) of these shops and the number of items that were bought in total.
The problem I am having is that I don't know how to link the name of the items to the values below it. So Python needs to know that as it is reading each line it should know what value to multiply the number beneath it. For example a potato costs 0.70 for one piece so it should multiply 0.7 by 30 and so on for the different items.
This is what I have coded so far:
user = input("Enter file name - AprilSales, MaySales, or JuneSales")
revenue = 0
response = None
try:
response = urllib.request.urlopen("link of server" + user + ".txt")
except:
print("File not found")
fileData = response.read().splitlines()
for i in range(len(fileData)):
fileData[i] = fileData[i].decode('utf-8')
for line in fileData:
if len(line) != 0:
#try block to only convert the strings which are able to be converted to ints and leave the rest
try:
print(int(line))
except:
print(line)
if(line == "Potatoes"):
revenue = revenue + int(line.next() * 0.7)
if(line == "Fish"):
revenue = revenue + int(line.next() * 1.50)
if(line == "Chicken"):
revenue = revenue + int(line.next() * 1.70)
else:
break
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用itertools模块来帮助我们处理文件,因为它们是由黑线分开的,因此
首先我们绘制了线条,我们会剥离每条线(以确保空行真的很空,没有那里有一些偷偷摸摸的空间,并从一个偷偷摸摸的空间中删除了一个不必要的空白空间,然后将其放入一个衬里,我们可以使用 map ),然后使用 groupby 将其拆分在黑线上。
现在,我们只需要处理每个组中的每个组,鉴于结构现在为[store_name,product1,narter1,produc2,nose2,...,...,生产,金额],我们只需要构建一种方便的方法来处理它,然后打包该功能成为一个函数。
但首先让 dictionary 现在,要将它们全部放在整洁的小地方,并从一个无尽的IF-Else Cascade中撕下撕裂,
现在让我们现在可以构建该功能
,我们可以将我们超越该功能的组馈送
,现在您可以将其保存在其他数据结构中现在
,所有内容都已包装和处理,询问全球收入很容易
知道特定商店中有多少商店出售了
it = iter(products)
,zip(zip)它,它)
具有以下效果:iter
制作事物的迭代器,那是我们想要的东西的一次,所以当zip消耗时,它不会达到同一元素再次使其与下一个元素与下一个元素配对,如果不是这种情况,
我们可以替换
products
process_data中的 counter 您无需使用。对于读者we can use the itertools module to help us process the file, given that they are separated by a black line like so
first we get the lines, we strip each one (to make sure that the empty line is really empty and doesn't have some sneaky space there, and remove unnecessary white space from the others one too, and to make that into a one liner we can use a map), then use groupby to split that on the on black line.
Now we just need to process each of those groups, given that the structure is now [store_name, produc1,amount1, produc2,amount2, ..., producn,amountn] we just need to build a convenient way to handle that, and pack that into a function.
But first let make a dictionary with all those prices to have them all in neat little place and get rip off an endless if-else cascade
now lets build that function
now we can feed the groups we get above to this function
and now in turn you can you can save it in another data structure like a dictionary yet again
Now that everything is packed and processed, asking for the global revenue is simple
to know how many items in total a particular store sold
the
it = iter(products)
,zip(it, it)
have the following effect:iter
make an iterator of the thing, that is a one time only iterable of the thingwhich we want so when zip consume it wouldn't hit the same element again making it possible to pair it with the next one, if that weren't the case we would get
You can replace the
products
dict in process_data for a Counter so you don't need to use .get method, but I leave that as an exercise for the reader