So what I have to do is figure out how to ask a user for a a text file and output the average of all the numbers. By tinkering around I have figured out how to find the average of a list i made but not sure how to do a list that the user give me. This is what I have right now:
with open('average', 'wt') as myFile:
myFile.write('3\n')
myFile.write('45\n')
myFile.write('83\n')
myFile.write('21\n')
with open('average', 'rt') as myFile:
total, n = 0, 0
for line in myFile:
total += int(line)
n += 1
print(float(total) / n)
Supposing that there is one number on each line of the file:
with open(input('Filename: '), 'r') as f:
numbers = [int(a.strip()) for a in f]
print('Average is {}'.format(sum(numbers)/len(numbers)))
Something like this?
import string
fileName = raw_input("What file name: ")
lines = []
try:
file = open(fileName)
lines = file.readlines()
file.close()
except:
print "Unable to open file"
sum = 0
values = 0
if(len(lines) > 0):
for line in lines:
value = 0
try:
value = int(string.strip(line))
except ValueError:
pass
if(value != 0):
sum = sum + value
values += 1
print "Average = %f for %d lines, sum = %f"%(sum/values,values,sum)
else:
print "No lines in the file"
NOTE: This assumes one number per line. It will not count blank lines or lines that have text. Other than that, junk on the lines or a bad file should not cause an exception, etc.
This was the test file (there are blank lines):
10
20
30
40
50
23
5
asdfadfs
s
And the output:
What file name: numbers.txt
Average = 25.000000 for 7 lines, sum = 178.000000
Related
I'm looking to have the program read a text file that is formatted like this for example.
Kristen
100
Maria
75
Frank
23
Is there anyway in python to skip lines and have it read only the numbers, accumulate them, and average them out? Could be more numbers or less numbers than the example above. I'm very much stuck.
you can use re.findall to find all numbers in a string:
import re
if __name__ == "__main__":
numbers = []
with open("./file.txt", "r") as f:
for line in f:
line = line.strip()
temp = list(map(lambda x: eval(x), re.findall(r'\d+', line)))
numbers += temp
average = sum(numbers) / len(numbers)
print(average)
This is the method I would use:
def get_average(filepath):
total = 0.0
with open(filepath, 'r') as f:
lines = f.readlines()
numbers = 0
for line in lines:
try:
number = int(line.strip())
total += number
numbers += 1
except:
continue
return total / float(numbers)
get_average("path/to/file.txt")
use strip to get rid of newline and isdigit to check for digit
In [8]: with open('a.txt', 'r') as f:
...: s = [int(i.strip()) for i in f if i.strip().isdigit()]
...:
In [9]: sum(s)/len(s)
Out[9]: 66.0
# assuming a score always follows a players name.
with open("path_to_file.ext", "r") as inf:
print(inf.readlines()[1::2]) # Do something with the result
# only grabbing lines that can be interpreted as numbers
with open("path_to_file.ext", "r") as inf:
for _ in inf.readlines():
if _.rstrip().isnumeric():
print(_.rstrip()) # Do something with the result
If the file name 'file.txt'
total = 0
i = 0
with open('file.txt', 'r') as file:
for line in file:
try:
total += int(line)
i += 1
except:
continue
average = total / i
So I'm trying to calculate the sum and average of a text document filled with 10000 numbers.ยจ
This is my code:
with open("\\Users\\saksa\\python_courses\\1DV501\\assign3\\file_10000integers_A.txt", "r") as f:
total = 0
number_of_ints = 0
for line in f:
for i in line:
if i.isdigit() == True:
total += int(i)
number_of_ints +=1
print (total)
print (number_of_ints)
The document is formated like: 215, 631, 731, 225, 315, etc in multiple lines
The problem is that it reads every number 1 by 1. So 100 becomes 1 + 0 + 0.
I think I need to use split to make it work but I cant figure out how to.
You can iterate over each line in a file.
And you can split each line by a ',' and add each number
with open("\\Users\\saksa\\python_courses\\1DV501\\assign3\\file_10000integers_A.txt", "r") as f:
total = 0
number_of_ints = 0
for line in f:
print(line)
for num in line.split(','):
print(num)
total += int(num)
number_of_ints += 1
print(total)
print(number_of_ints)
You will need to add some logic to ensure the numbers are numbers
You should use split() function:
line = "110, 23400, 34569, 23567"
line.replace(" ", "") # Get rid of unnecessary spacebars
total = 0
number_of_ints = 0
for i in line.split(","):
# Here you can do whatever you want
try:
i = int(i)
total += int(i)
number_of_ints += 1
except:
pass
I have written a code that extracts floating point numbers from a
text file and produces a list of the numbers.
My challenge is summing the consecutive numbers and finding the
average of the numbers.
I am not allowed to use the sum function and I am new to python ..
this the code I have written so far ,
what can I do to add through the list
fh = open(fname)
for line in fh:
if line.startswith("X-DSPAM-Confidence:") : continue
# print(line)
count = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
count = count + 1
# print(count)
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
# print(line)
xpos = line.find(' ')
# print(xpos)
num = line[xpos : ]
# print(float(num))
fnum = float(num)
# print(fnum)
total = 0
for i in fnum:
total += int(i)
print(total)
Error:"float object not iterable on line 24" ... line 24 is the 4th for loop
First an open file is iterable only once, and your code shows 4 loops starting with for line in fh:. After first loop, the file pointer will reach the end of file, and the following loops should immediately return. For that reason with should be prefered.
Next somewhere in the loop you get a float value in fnum. Just initialize total before starting the loop, and add fnum when you get it:
total = 0
with open(fname) as fh:
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
# print(line)
xpos = line.find(' ')
# print(xpos)
num = line[xpos : ]
# print(float(num))
fnum = float(num)
# print(fnum)
total += fnum
# print(total)
with ensures that the file will be cleanly closed at the end of the loop.
fnum is a float. It's not an array, therefore it's not iterable and cannot be iterated in a for loop.
You probably don't need an array to determine the total and the average:
fname = "c:\\mbox-short.txt"
fh = open(fname)
count = 0
total = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
xpos = line.find(' ')
num = line[xpos : ]
fnum = float(num)
total += fnum
count += 1
print("Total = " + str(total))
print("Average = " + str(total / count))
print("Number of items = " + str(count))
You don't have to use startsWith in this case. Better to use split for each line of the file. Each line will split all the words to a list. Then using the indexes you look for, X-DSPAM-Confidence:. If it exists then take the corresponding value of interest. In this case it is index number 1. Below is the code:
total = 0
number_of_items = 0
with open("dat.txt", 'r') as f:
for line in f:
fields = line.split()
if fields != []:
if fields[0] == "X-DSPAM-Confidence:":
number_of_items += 1
total += float(fields[1])
print(total)
print(number_of_items)
avg = (total/number_of_items)
print(avg)
I saved your data in a text file names, "dat.txt".
Hope it helps !!!
The file looks like this:
1, a b
2, c d
3, e f
my current code
b = open('file.txt', 'r')
c = b.readlines()
regels = len(c)
print(regels)
I got the numbers of lines but still need biggest number + on which line it is.
So you are just looking to find the biggest number in the first column of the file? This should help
b = open('file.txt', 'r')
c = b.readlines()
regels = len(c)
print(regels)
max = 0
for line in b.readlines():
num = int(line.split(",")[0])
if (max < num):
max = num
print(max)
# Close file
b.close()
This is how I'd go about doing it.
max_num = 0
with open('file.txt', 'r') as data: # use the with context so that the file closes gracefully
for line in data.readlines(): # read the lines as a generator to be nice to my memory
try:
val = int(line.split(",")[0])
except ValueError: # just incase the text file is not formatted like your example
val = 0
if val > max_num: # logic
max_num = val
print max_num #result
You need loop over each line in file, parse each line and find the largest number.
I do not quite understand how the numbers are stored in your file. Just assuming that in each line, the first field are numeric and separate with others (non-numeric) by ','. And I assume all numbers are integer.
ln = 0
maxln = 0
maxn = 0
with open(filename, 'r') as f:
line = f.next()
if line:
ln = 1
maxln = 1
maxn = int(line.split(",")[0].strip())
else:
raise Exception('Empty content')
for line in f:
ln += 1
cur = int(line.split(",")[0].strip())
if cur > maxn:
maxn = cur
maxln = ln
ln is used to record current line number, maxn is used to record current maximum number, and maxln is used to record current maximum number location.
One thing you need to do is fetch the first line to initialize these variables.
None of the answers give you the line of the max number so I'll post some quick code and refine later
max_num = 0
line_count = 0
with open('file.txt', 'r') as infile:
for line in infile:
number = int(line.split(',')[0])
if number > max_num:
max_num = number
line_num = line_count
line_count += 1
print (max_num)
print (line_num)
Read line
Split it on basis of comma
Append first element to temp list.
Once complete reading of file is done,
To get maximum number, just use max function on temp list.
Since file is read line by line sequentially and appending number from line to temp list, to get line number on which maximum number is present, just find the index of max number in temp list and increment it by one since list index starts with zero.
P.S : Check last three print statements
Code:
num_list = []
with open('master.csv','r')as fh:
for line in fh.readlines():
num_list.append(int((line.split(','))[0]))
print num_list
print "Max number is -" ,max(num_list)
print "Line number is - ", (num_list.index(max(num_list)))+1
Output:
C:\Users\dinesh_pundkar\Desktop>python c.py
[1, 2, 3]
Max number is - 3
Line number is - 3
C:\Users\dinesh_pundkar\Desktop>
Iterate through the file and keep track of the highest number you've seen and the line you found it on. Just replace this with the new number and new line number when you see a bigger one.
b = open('file.txt', 'r')
max = -1
lineNum = -1
line = b.readline()
index = 0
while(line):
index+=1
newNum = line[0]
if(newNum>max):
max = newNum
lineNum = index
line = b.readline()
print lineNum,max,index
max is your highest number, lineNum is where it was, and index is the number of lines in the file
The program should output all of the integers, one per line, with no blank lines between each line. This program should also output the largest random number that was on file.
myfile = open('mynumbers.txt', 'r')
lines = myfile.readline()
print(lines)
I have gotten that far and I'm stuck. I have literally been sitting here for 6 hours and I don't know what the deal is!
I need help using a loop to read and process the mynumbers.txt file and then it also has to display the largest number that was in the group.
myfile = open('mynumbers.txt', 'w')
import random
num = random.randint(6, 12)
print(num)
for num in range(num):
myfile.write(str(random.randrange(10, 20)))
I also keep getting this error after I try everything.
ValueError: invalid literal for int() with base 10: '16 19 11 18 14 11 15 18 18 16 20 16'
Sorry everyone i'm new to the site!
.readline() only read one line.
You should use .readlines() if you want to get all the lines of your file.
Moreover, it is better to open your file using with.
with open('filename.txt') as fp:
lines = fp.readlines()
# Do something with the lines
See the documentation for more information.
use myfile.readlines() instead of myfile.readline()
max = lines[0]
for line in lines:
print line
print "MAX : ", max([int(line) for line in lines])
First of all, in your write code, you should be using
myfile.write(str(random.randrange(10, 20)) + '\n')
The reason you are getting an error message is because all of your numbers are being written to the same line.
If you want to keep your write code then use the following.
with open('mynumbers.txt', 'r') as myfile:
line = myfile.readline()
nums = line.split(' ')
# Set first number as the max
max = int(nums[0])
# Converting to int will remove trailing newline
print(max)
# Go through the rest of the numbers
for i in range(1, len(nums)):
x = int(nums[i])
print(x)
# Check max
if x > max:
max = x
print("Max = " + str(max))
else use this after changing the write code.
with open('mynumbers.txt', 'r') as myfile:
# Put each line into an array
lines = myfile.readlines()
# Set first number as the max
max = int(lines[0])
# Converting to int will remove trailing newline
print(max)
# Go through the rest of the numbers
for i in range(1, len(lines)):
x = int(lines[i])
print(x)
# Check max
if x > max:
max = x
print("Max = " + str(max))
I would recommend
max([int(l) for l in myfile.readlines()])
edit:
according to your file format, something like this will probably work
max([int(l) for l in myfile.readline().split()])
Your sample file creation script will cause all of your numbers to be created as one long line. You would need to add a newline after each write.
myfile = open('mynumbers.txt', 'w')
import random
num = random.randint(6, 12)
print(num)
for num in range(num):
myfile.write("%s\n" % random.randrange(10, 20))
The following will produce the answer you want. Contrary to other suggestions, I would recommend also learning about processing such files a line at a time as this would scale better. Say in the future your test file was huge, then attempting to read the whole file in could result in you running out of memory to process it. By loading the file a line at a time, it would be able to cope with any size.
max_value = None
with open('mynumbers.txt', 'r') as myfile:
for line in myfile:
num = int(line)
if max_value:
max_value = max(max_value, num)
else:
# Use the first value as the initial max value
max_value = num
if max_value:
print("Max value: %u" % max_value)
else:
print("No numbers found")
Python2 If this is what you have in your text file:16 19 11 18 14 11 15 18 18 16 20 16
You can find the largest number like this:
fname = 'mynumbers.txt'
fhand = open(fname)
for line in fhand:
line = line.rstrip()
num =[int(s) for s in line.split(' ')]
print max(num)
Output:
20
To print all the numbers:
for i in num:
print i
Output:
16
19
11
18
14
11
15
18
18
16
20
16