You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何从文本文件特定行提取整数并计算周末平均气温

问题解决:提取周末气温并计算平均值

任务需求

编写Python程序完成以下操作:

  • 从互联网下载文本文件
  • 在文件中提取“Saturday”和“Sunday”对应的气温数值
  • 计算周末平均气温,且不得修改原文本文件

文本文件内容示例:

Thursday: 21
Friday: 19
Saturday: 13
Sunday: 17
Monday: 19
Tuesday: 21
Wednesday: 16

用户现有代码

import urllib.request

my_list = list()
word1 = "Saturday"
word2 = "Sunday"
temp1 = 0
temp2 = 0
average = (temp1 + temp2) / 2

urllib.request.urlretrieve("https://www.senarclens.eu/~gerald/teaching/cms/data/fc.txt", "fc.txt")

open('fc.txt', 'r')
with open('fc.txt')  as f:
    lines = f.read().split("\n")

for i, line in enumerate(lines):
    if word1 in line:
        print("Word \"{}\" found in line {}".format(word1, i + 1))
for i, line in enumerate(lines):
    if word2 in line:
        print("Word \"{}\" found in line {}".format(word2, i + 1))
# need the temperature next to the name of day for temp1, temp2

if average > 25:
    print("Next weekend, swimming would be a good activity.")
elif average >= 12 and average < 25:
    print("Next weekend, hiking would be a good activity.")
elif average >= 5 and average < 12:
    print("Next weekend, watching movies would be a good activity.")
elif average >= -5 and average < 5:
    print("Next weekend, relaxing in the local hot springs would be a good activity.")
else:
    print("Next weekend, skiing would be a good activity.")

解决方法

要提取气温数值,核心是对包含目标日期的行做字符串分割处理,具体实现如下:

  1. 分割行内容:每行格式为日期: 气温,用: 作为分隔符拆分行,第二部分即为气温字符串。
  2. 类型转换:将提取到的气温字符串转为整数,赋值给对应的temp变量。
  3. 优化循环:合并两次遍历操作,一次循环即可处理周六和周日的行。

修改后的关键代码片段:

# 替换原有两次循环,一次遍历完成气温提取
for i, line in enumerate(lines):
    if word1 in line:
        print("Word \"{}\" found in line {}".format(word1, i + 1))
        temp1 = int(line.split(': ')[1])
    if word2 in line:
        print("Word \"{}\" found in line {}".format(word2, i + 1))
        temp2 = int(line.split(': ')[1])

# 必须在获取到真实气温后再计算平均值
average = (temp1 + temp2) / 2

完整修正代码

import urllib.request

word1 = "Saturday"
word2 = "Sunday"
temp1 = 0
temp2 = 0

# 下载目标文件
urllib.request.urlretrieve("https://www.senarclens.eu/~gerald/teaching/cms/data/fc.txt", "fc.txt")

# 读取文件内容
with open('fc.txt') as f:
    lines = f.read().split("\n")

# 遍历提取周末气温
for i, line in enumerate(lines):
    if word1 in line:
        print("Word \"{}\" found in line {}".format(word1, i + 1))
        temp1 = int(line.split(': ')[1])
    if word2 in line:
        print("Word \"{}\" found in line {}".format(word2, i + 1))
        temp2 = int(line.split(': ')[1])

# 计算平均气温
average = (temp1 + temp2) / 2

# 根据气温输出活动建议
if average > 25:
    print("Next weekend, swimming would be a good activity.")
elif average >= 12 and average < 25:
    print("Next weekend, hiking would be a good activity.")
elif average >= 5 and average < 12:
    print("Next weekend, watching movies would be a good activity.")
elif average >= -5 and average < 5:
    print("Next weekend, relaxing in the local hot springs would be a good activity.")
else:
    print("Next weekend, skiing would be a good activity.")

补充说明

  • 用line.split(': ')分割是为了匹配文件中冒号后带空格的格式,确保准确提取气温
  • 转换整数时,基于文件格式的规范性可以直接转换,若需容错可添加异常处理
  • 平均值计算必须放在气温提取之后,否则会使用初始值0导致结果错误

内容的提问来源于stack exchange,提问作者HR_AUT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 16:21:12