Reputation: 23
I am trying to gather numbers from a simple text file and compute the average of those numbers. However, I need two separate averages from the text file. The text file would be in this format.
random string
num 1
num 2
random string
random string
num 1
num 2
random string
random string
num 1
num 2
random string
random string
num 1
num 2
random string
... and so on and so forth, the pattern would always be this way.
I'd need to be able to get the average of all the num1's, then the average of all the num2's separately. The numbers would be integers. What method could I use to do this? Maybe adding them to an array or list and calculating them that way? Or is there another way?
Any guidance or advice would be much appreciated.
Upvotes: 2
Views: 498
Reputation: 696
start by opening the file then read the first line. Then use a for loop and add all the numbers to a total then divide by the total amount of numbers read.
import os
PATH = os.path.dirname(__file__)
with open(PATH+r"\your_text_file.txt", "r") as file:
total_first = 0
total_second = 0
cardinal = 0 # The cardinal is the number of elements in a set.
for line in file: # I know that there is a reccurent pattern in the text file.
try:
int(line)
except ValueError: # If the line is a string, read the next 2 lines as integers then read a third line and start over.
cardinal += 1
total_first += int(file.readline())
total_second += int(file.readline())
file.readline()
average_first = total_first / cardinal
average_second = total_second / cardinal
Upvotes: 1
Reputation: 24518
You can use linecache.
import linecache
line = linecache.getline(file.txt, line_number) # Note: first line is 1, not 0
Once you can access any line, calculating the averages is trivial.
Upvotes: 0
Reputation: 1227
You can use regex to extract all the numbers and then work around that.
import re
with open('test.txt') as file:
s = file.read()
nums = re.findall(r'\d', s)
nums = [int(num) for num in nums]
nums.sort()
print(nums)
This will give you a list of all integers in your text file in ascending order.
Upvotes: 0
Reputation: 18406
text = '''random string
1
2
random string
random string
3
4
random string
random string
5
6
random string
random string
7
8
random string'''
You can use regular expression, and get first, and second numbers in the pairs to two different lists, then perform the remaining operations:
first=[]
second=[]
for m in re.finditer(r'\n(\d+)\n(\d+)\n', text):
first.append(int(m.group(1)))
second.append(int(m.group(2)))
OUTPUT:
# The values captured from the text string
>>>first, second
([1, 3, 5, 7], [2, 4, 6, 8])
# Average of first values in the pairs
>>> (sum(first)/len(first))
4.0
# Average of second values in the pairs
>>> (sum(second)/len(second))
5.0
Upvotes: 2