I have a code that relies on me reading a text file, printing off the numbers where there are numbers, printing off specific error messages where there are strings instead of numbers, then summing ALL the numbers up and printing their sum (then saving ONLY the numbers to a new text file).
I have been attempting this problem for hours, and I have what is written below.
I do not know why my code does not seem to be summing up properly.
And the python code:
f=open("C:\\Users\\Emily\\Documents\\not_just_numbers.txt", "r")
s=f.readlines()
p=str(s)
for line in s:
printnum=0
try:
printnum+=float(line)
print("Adding:", printnum)
except ValueError:
print("Invalid Literal for Int() With Base 10:", ValueError)
for line in s:
if p.isdigit():
total=0
for number in s:
total+=int(number)
print("The sum is:", total)
I have a code that relies on me reading a text file, printing off the
numbers where there are numbers, printing off specific error messages
where there are strings instead of numbers, then summing ALL the
numbers up and printing their sum (then saving ONLY the numbers to a
new text file).
So you have to do the following:
Print numbers
Print a message when there isn't a number
Sum the numbers and print the sum
Save only the numbers to a new file
Here is one approach:
total = 0
with open('input.txt', 'r') as inp, open('output.txt', 'w') as outp:
for line in inp:
try:
num = float(line)
total += num
outp.write(line)
except ValueError:
print('{} is not a number!'.format(line))
print('Total of all numbers: {}'.format(total))
This is a very short way to sum all numbers in your file (you will have to add try and except)
import re
print(sum(float(num) for num in re.findall('[0-9]+', open("C:\\Users\\Emily\\Documents\\not_just_numbers.txt", 'r').read())))
You are checking the wrong condition:
for line in s:
if p.isdigit():
p is this:
s=f.readlines()
p=str(s)
Being a strified version of a list, it will start with a '[', and hence p.isdigit() will always be false. You instead want to check line.isdigit(), and you want to only initialise total once instead of each time around the loop:
total = 0
for line in f:
if line.isdigit():
total += int(line)
Note that by iterating over f directly, you also don't need to ever call readlines().
Here is what you can do:
data.txt:
1
2
hello
3
world
4
code:
total = 0
with open('data.txt') as infile:
with open('results.txt', 'w') as outfile:
for line in infile:
try:
num = int(line)
total += num
print(num, file=outfile)
except ValueError:
print(
"'{}' is not a number".format(line.rstrip())
)
print(total)
--output:--
'hello' is not a number
'world' is not a number
10
$ cat results.txt
1
2
3
4
you can also try this:
f=open("C:\\Users\\Emily\\Documents\\not_just_numbers.txt", "r")
ww=open("C:\\Users\\Emily\\Documents\\not_just_numbers_out.txt", "w")
s=f.readlines()
p=str(s)
for line in s:
#printnum=0
try:
#printnum+=float(line)
print("Adding:", float(line))
ww.write(line)
except ValueError:
print("Invalid Literal for Int() With Base 10:", ValueError)
total=0
for line in s:
if line.strip().isdigit():
total += int(line)
print("The sum is:", total)
here str.strip([chars]) means
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped
Every time you enter a new line you reset the total to zero if the number is a digit.
You might want your total to initialize before you enter the loop.
I tried debugging the for loop using the isdigit and isalpha
apparently every new line is not considered a digit or alphanumeric these always evaluate to false
As it turns out you don't need the for loop you've done most of the program with your try except statement
Here's how I did it on my system.
f = open("/home/david/Desktop/not_just_numbers.txt", 'r')
s = f.readlines()
p = str(s)
total = 0
for line in s:
#print(int(line))
printnum = 0
try:
printnum += float(line)
total += printnum
#print("Adding: ", printnum)
except ValueError:
print("Invalid Literal for Int() With Base 10:", ValueError)
print("The sum is: ", total)
$ echo -e '1/n2/n3/n4/n5' | python -c "import sys; print sum(int(l) for l in sys.stdin)"
Read from file containing numbers separated by new lines:
total = 0
with open("file_with_numbers.txt", "r") as f:
for line in f:
total += int(line)
print(total)
Related
Basically, this is my task. Extract numbers from a text file and then calculate the sum of them.
I wrote the code successfully and but it doesn't work fine with 2 or more digit numbers and negative numbers. What should i do?
f = open('file6.txt', 'r')
suma = 0
file = f.readlines()
for line in file:
for i in line:
if i.isdigit() == True:
suma += int(i)
print("The sum is ", suma)
file6.txt:
1
10
Output:
The sum is 2
In your case, you are going line by line first through the loop and looking at every digit ( in second loop ) to add.
And /n at the end of elements make the .isDigit() function disabled to find the digits.
So your updated code should be like this :
f = open('file6.txt', 'r')
suma = 0
file = f.readlines()
for line in file:
if line.strip().isdigit():
suma += int(line)
print("The sum is ", suma)
Hope it helps!
Use re.split to split the input into words on anything that is not part of a number. Try to convert the words into numbers, silently skip if this fails.
import re
sum_nums_in_file = 0
with open('file6.txt') as f:
for line in f:
for word in re.split(r'[^-+\dEe.]+', line):
try:
num = float(word)
sum_nums_in_file += num
except:
pass
print(f"The sum is {sum_nums_in_file}")
This works for example on files such as this:
-1 2.0e0
+3.0
i want to add ints that is stored in a file and add the numbers to find the total.. i took this code from a txt book but im getting the following error :
invalid literal for int() with base 10: '1 2 3 12 4 55'
how can i solve this if i want to read the numbers one by 1 not by using a list
f = open("intgers.txt", 'r')
theSum = 0
for line in f:
line = line.strip()
number = int(line)
theSum += number
print("The sum is", theSum)
You need to split the line.
f = open("intgers.txt", 'r')
theSum = 0
for line in f:
numbers = line.strip().split(' ')
for number in numbers:
n = int(number)
theSum += n
print("The sum is", theSum)
Using this code block, you're assuming each of the numbers are separated by new lines in intgers.txt. As the error suggests, the line variable is '1 2 3 12 4 55'. You can't convert that to an integer. You need to parse this into a list of numbers first:
f = open("intgers.txt", 'r')
theSum = 0
for line in f:
numbers = line.split()
for num in numbers:
number = int(num.strip())
theSum += number
print("The sum is", theSum)
Look into uses of string methods strip() and split() if you're confused.
It appears that there are multiple space-separate integers on each line. You need to .split() each line and iterate over the resulting list to add the numbers from that line.
f = open("intgers.txt", 'r')
theSum = 0
for line in f:
splitline = line.strip().split()
for item in splitline:
number = int(item)
theSum += number
print("The sum is", theSum)
There are a number of improvements that can be made to this, using map to apply the int function across the list and using implicit iteration to get a sum directly from each line. For interest:
f = open("intgers.txt", 'r')
theSum = 0
for line in f:
theSum += sum(map(int, line.strip().split()))
print("The sum is", theSum)
Hello I'm a few weeks into python and now learning files. I've made the program be able to sum the numbers in the file if there were only numbers but now there are numbers aswell as words. How do I make it ignore the words and make it sum to 186?
def sum_numbers_in_file(filename):
"""reads all the numbers in a file and returns the sum of the numbers"""
filename = open(filename)
lines = filename.readlines()
result = 0
for num in lines:
result = result + int(num)
num.rstrip()
filename.close()
return result
answer = sum_numbers_in_file('sum_nums_test_01.txt')
print(answer)
This is in the file:
1
Pango
2
Whero
3
4
10
Kikorangi
20
40
100
-3
4
5
You can easily add a try-except statement inside the function to make it work only on numbers:
def sum_numbers_in_file(filename):
"""reads all the numbers in a file and returns the sum of the numbers"""
filename = open(filename)
lines = filename.readlines()
result = 0
for num in lines:
try:
result = result + int(num)
num.rstrip()
except ValueError:
pass
filename.close()
return result
answer = sum_numbers_in_file('sum_nums_test_01.txt')
print(answer)
Or you can use the isalpha method:
def sum_numbers_in_file(filename):
"""reads all the numbers in a file and returns the sum of the numbers"""
filename = open(filename)
lines = filename.readlines()
result = 0
for num in lines:
num = num.rstrip()
if not num.isalpha():
result = result + int(num)
filename.close()
return result
answer = sum_numbers_in_file('sum_nums_test_01.txt')
print(answer)
The isalpha() returns true only if the string doesn't contain symbols or numbers, so you can use it to check if the string is a number. Also works on decimal numbers.
Note that it also detects symbols as numbers, so if there's a symbol in the line it will count that as a number, potentially generating errors!
You can use a try-except block, an advanced yet effective way of preventing errors. Add this in your for loop:
try:
result += int(num)
except: pass
Normally it's a good practice to add something in the except clause but we don't want anything so we just pass. The trymeans we try but if we fail we go to the except part.
I would suggest using a try/except block:
with open("words.txt") as f:
nums = []
for l in f:
try:
nums.append(float(l))
except ValueError:
pass
result = sum(nums)
A simple one-liner that you could implement to get all numerical values if you want an alternative would be:
with open("words.txt") as f:
nums = [float(l.strip()) for l in f if not l.strip().isalpha()]
result = sum(nums)
Here, I convert each line into a float and append that value to the nums list. If the line is not a numerical value, it will simply just be passed over, hence pass.
You cannot use .isnumeric() as it will only work for strings that contain only integers. This means no decimals or negative numbers.
Here are couple of way's you can try using isdigit,
value = 0
with open("sum_nums_test_01.txt") as f:
for l in f.readlines():
if l.strip().isdigit():
value += int(l)
with open("sum_nums_test_01.txt") as f:
value = sum(int(f) for f in f.readlines() if f.strip().isdigit())
Goal: To calculate the total of all the numbers in a text file(Refer file in link at the end). The output sum should be 313125
Problem: Option 1 returns an empty list for re.findall and thus the total is zero. I think the problem maybe that 'line' variable is not read as a string. Option 2 works as expected.
Help needed. What is wrong with the code in Option1?
Option1 :
import re
# read the file
fh = open(r"regex_sum_395835.txt", encoding='utf-8')
for lin in fh: # loop to read each line in the file
line = str(lin).strip()
array = re.findall('[0-9]+',line) **# I think this is where the problem exists.**
print("Array is", array) *# test line to print the contents of list. It returns and empty list*
total = 0
for number in array: *# loop through the list to find total of all numbers*
total = total + int(number)
print("Sum is", total) *# print the total of all numbers*
Option 2: This option works, but I want to understand why first option did not work
import re
fh = open(r"regex_sum_395835.txt", encoding='utf-8').read()
array = re.findall('[0-9]+',fh)
total = 0
for number in array:
total = total + int(number)
print("Sum is", total)
Link to text file
The code is reassigning array for every line. The last line of the file has no numbers so the final value is an empty list.
Rearrange like so to get the answer:
import re
# read the file
with open(r"regex_sum_395835.txt", encoding='utf-8') as fh:
total = 0
for line in fh: # loop to read each line in the file
array = re.findall('[0-9]+',line) # I think this is where the problem exists.**
for number in array: # loop through the list to find total of all numbers*
total += int(number)
print("Sum is", total) # print the total of all numbers*
I've been getting stuck in the total_num() function as it gives the error
"ValueError: invalid literal for int() with base 10: ''"
I know how to do it if its a defined list but if its set by the user I get confused.
def total_num():
total = 0
num_file = open("num_list.txt", "r")
line = num_file.read()
while line != "":
num1 = int(num_file.readline())
total = total + num1
print total
def read_num():
num_file = open("num_list.txt", "r")
for line in num_file:
print line.rstrip("\n")
def write_num():
num = input("Enter a number: ")
num_file = open("num_list.txt", "w")
num_consec = 0
for x in range(num):
num_consec = num_consec + 1
num_file.write(str(num_consec)+ "\n")
num_file.close()
def main():
write_num()
read_num()
total_num()
main()
The error is because you are getting an empty string from your text file. Look at this bit of code; you are reading the whole file into memory.
line = num_file.read()
while line != "":
Over here, unless you opened an empty file line != "" you are comparing the whole file with an empty string. So you will keep on looping until your num1 = int(num_file.readline()) reads an empty line from the file.
You can find the solution if you look at your read_num method.
for line in num_file:
try:
total += int(line)
except ValueError:
print "Invalid data in ", line
By using try except you are able to handle the situation where the file might contain other invalid texts.
You are reading the file in an odd way - namely, twice. read() puts the entire file contents into a string. If you repeatedly check whether there are characters in it, and then never change it, it will either not execute or loop infinitely.
Using input() to get a number will work, but it's better to use raw_input() and cast with int() for safety. Also, xrange() is a better practice than range() in Python 2. You don't need to keep a manual counter if you're already iterating over range(), either.
Overall, your code could be condensed to this:
def write_num():
num = int(raw_input("Enter a number: "))
with open("num_list.txt", "w") as output:
for x in xrange(1, num+1):
output.write(str(x) + "\n")
def read_num():
with open("num_list.txt") as f:
numbers = map(int, f)
for number in numbers:
print number
return numbers
def main():
write_num()
print sum(read_num())
main()