How to get an Integer from a specific line of a text file in Python?

Question:

I need some help with a task in python.
The task is to make a program that downloads a text file from the internet.
Then you have to find "Saturday" and "Sunday" in the text file.
Next to those are the temperatures.
The task is to calculate the average temperature of the weekend.
I am not allowed to change the text file.

Here is an example of the text file contents:

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

well… i found which line they are but i don’t know how to get the temperatures. I need to seperate the numbers from the text and assign them to my two variables temp1 and temp2.
I would be grateful if you could help me.
(sorry for my english. i am not a native speaker.)

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.")

Asked By: HR_AUT

||

Answers:

If the text file is always going to be in this format, you can use a split function and not worry about regex ever.

You won’t need two for loops

daily_temps = {}

for i, line in enumerate(lines):
    splits = line.split(":")
    day = splits[0].strip()
    temp = int(splits[-1].strip())
    daily_temps[day] = temp

The strip removes any extra spaces

This will give you

{'Thursday': 25, 'Friday': 19, 'Saturday': 13 ...}

Then you do

temp1 = daily_temps['Saturday']

temp2 = daily_temps['Sunday']

average = (temp1 + temp2) / 2

if average > 25:
    print("Next weekend, swimming would be a good activity.")

The average declaration you have before you set temp1 and temp2 is wrong. Declare it after they’ve been set.

Answered By: surge10

You can try splitting each line with the ‘:’ character, and get the second part of the returned list.

Like you split the lines on the txt file.

Answered By: Chris_Arg

Your goal is to get integers, so basically you don’t need to enumerate lines by yourself. This code will help you get what you need:

import re
data = """Friday: 10
Sunday: 12,
...
Sunday: 33,
"""
>>> re.findall(r'Sunday:sd{1,3}', data, re.MULTILINE)
['Sunday: 12', 'Sunday: 33']
Answered By: Alex Bender

This is a common scraping scenario, something that is nice to train on, using requests, beautifulsoup, scrapy, selenium, etc, good luck with your endeavor:

#!/usr/bin/env python
import requests,re
url = "https://www.senarclens.eu/~gerald/teaching/cms/data/fc.txt"
r = requests.get(url)
dat = r.text
out = {}
for l in dat.split('n'):
    m = re.search(r'(w+):s+(d+)', l)
    if m and m.group(2):
        out[m.group(1)] = int(m.group(2))
print(out)

giving:

{'Thursday': 21, 'Friday': 19, 'Saturday': 13, 
'Sunday': 17, 'Monday': 19, 'Tuesday': 21, 'Wednesday': 16}
Answered By: MortenB

In this case you can use .split() method to split word1 and word2 to separate the number from the text, then you can take the average by using only the number part by indexing it, and then turning it into an int:

temp1 = word1.split(":")
temp2 = word2.split(":")
average = (int(temp1[1]) + int(temp2[1])) / 2
Answered By: Jakis
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.