问题如下; “编写Python程序以读取包含湖泊和鱼类数据的文件,并设置报告 表格格式的湖泊识别号,湖泊名称和鱼重(使用 带格式的字符串区域)。该程序应计算平均鱼重 报告。”
湖泊识别;
1000 Chemo
1100 Greene
1200 Toddy
我必须阅读的文件“ FishWeights.txt”包含以下数据;
1000 4.0
1100 2.0
1200 1.5
1000 2.0
1000 2.2
1100 1.9
1200 2.8
我的代码;
f = open("fishweights.txt")
print(f.read(4), "Chemo", f.readline(4))
print(f.read(5), "Greene", f.read(5))
print(f.read(4), "Toddy", f.read(5))
print(f.read(5), "Chemo", f.read(4))
print(f.read(5), "Chemo", f.read(4))
print(f.read(5), "Greene", f.read(4))
print(f.read(5), "Toddy", f.read(4))
我收到的输出是;
1000 Chemo 4.0
1100 Greene 2.0
1200 Toddy 1.5
1000 Chemo 2.0
1000 Chemo 2.2
1100 Greene 1.9
1200 Toddy 2.8
在一定程度上我必须显示湖泊的ID号,名称和每个湖泊的鱼重,这是正确的。但是我需要能够计算出最终所有鱼的重量的平均值。 输出应整齐格式化,外观如下;
1000 Chemo 4.0
1100 Greene 2.0
1200 Toddy 1.5
1000 Chemo 2.0
1000 Chemo 2.2
1100 Greene 1.9
1200 Toddy 2.8
The average fish weight is: 2.34
感谢您的帮助,这里只是一名初学者,他们寻求帮助以全面了解该主题。谢谢!
答案 0 :(得分:1)
是的,您需要遍历行。这是您要寻找的结构:
with open("fishweights.txt") as fo:
for line in fo:
pass
现在,为了检索每行的每一段,您可以使用line.split()
。假设id的长度是固定的,那么读取固定数量的字节(如您所做的)是很好的。您确定每个ID始终都是4位数字吗?这样的事情可能会更好:
raw_data = []
with open("fishweights.txt") as fo:
for line in fo:
row = line.strip().split()
if not row:
continue # ignore empty lines
id = int(row[0])
no = float(row[1])
raw_data.append((id, no))
现在您拥有原始数据,需要对其进行汇总:
sum = 0
count = 0
for id, no in raw_data:
sum += no
count += 1
avg = sum / count
或单线
avg = sum(no for id, no in raw_data) / len(raw_data)
最后,您需要将ID映射到名称以进行最终打印:
id_to_name = {
1000: 'Chemo',
1100: 'Greene',
1200: 'Toddy',
}
for id, no in raw_data:
print(id, id_to_name[id], no)
print('Average: ', avg)
当然,所有三个循环都可以组合成一个循环。我对其进行了划分,以便您可以清楚地看到代码的每个阶段。最终的结果(略有优化)可能如下所示:
id_to_name = {
1000: 'Chemo',
1100: 'Greene',
1200: 'Toddy',
}
sum = 0
count = 0
with open("fishweights.txt") as fo:
for line in fo:
row = line.strip().split()
if not row:
continue # ignore empty lines
id = int(row[0])
no = float(row[1])
sum += no
count += 1
print(id, id_to_name[id], no)
print('Average:', sum/count)
答案 1 :(得分:0)
您不需要使用偏移量来读取行。另外,您可以使用public Integer multiply(Integer first, Integer second, Integer result){
return first * second;
}
确保完成后关闭文件。对于平均值,您可以将所有数字放在列表中,然后在末尾找到平均值。使用字典将湖泊ID映射到名称:
with
输出:
lakes = {
1000: "Chemo",
1100: "Greene",
1200: "Toddy"
}
allWeights = []
with open("test.txt", "r") as f:
for line in f:
line = line.strip() # get rid of any whitespace at the end of the line
line = line.split()
lake, weight = line
lake = int(lake)
weight = float(weight)
print(lake, lakes[lake], weight, sep="\t")
allWeights.append(weight)
avg = sum(allWeights) / len(allWeights)
print("The average fish weight is: {0:.2f}".format(avg)) # format to 2 decimal places
有更有效的方法来执行此操作,但这可能是最简单的方法来帮助您了解正在发生的事情。
答案 2 :(得分:0)
您可以将湖泊名称存储到字典中,并将数据存储在列表中。在此示例中,您仅需从那里遍历列表fish
并获取与id
对应的湖泊名称。最后,只需将列表中的weight
加起来并除以fish
的长度,就可以在下面打印平均值。
with open('LakeID.txt','r') as l:
lake = l.readlines()
lake = dict([i.rstrip('\n').split() for i in lake])
with open('FishWeights.txt','r') as f:
fish = f.readlines()
fish = [i.rstrip('\n').split() for i in fish]
for i in fish:
print(i[0],lake[i[0]],i[1])
print('The total average is {}'.format(sum(float(i[1]) for i in fish)/len(fish)))
还建议您使用with open(..)
上下文管理器,以确保文件退出时已关闭。
答案 3 :(得分:0)
因此,您可以在此处将鱼的体重和湖泊数据存储在两个阵列中。请参阅以下内容,其中读取每行,然后将它们分成鱼的重量列表和湖泊数据列表。
text=f.readlines()
fishWeights=[]
lakeData=[]
for item in text:
fishWeights.append(item.split(' ')[1])
lakeData.append(item.split(' ')[1])
您可以从此处输出信息
for i in range(len(fishWeights)) :
print(lakeData[i], "Your Text", fishWeights[i])
您可以计算出平均值
total=0
for weight in fishWeights:
total+=weight
total/=len(fishWeights)
答案 4 :(得分:0)
可以使用数据框轻松实现。 请在下面找到示例代码。
import pandas as pd
# load lake data into a dataframe
lakeDF = pd.read_csv('Lake.txt', sep=" ", header=None)
lakeDF.columns = ["Lake ID", "Lake Name"]
#load fish data into a dataframe
fishWeightDF = pd.read_csv('FishWeights.txt', sep=" ", header=None)
fishWeightDF.columns = ["Lake ID", "Fish Weight"]
#sort fishweight with 'Lake ID' (common field in both lake and fish)
fishWeightDF = fishWeightDF.sort_values(by= ['Lake ID'],ascending=True)
# join fish with lake
mergedFrame = pd.merge_asof(
fishWeightDF, lakeDF,
on='Lake ID'
)
#print the result
print(mergedFrame)
#find the average
average = mergedFrame['Fish Weight'].mean()
print(average)