Python count numbers from input file - python

I want to write a Python 3 script to read in an input file, like this:
8 13 67 43 47
9 19 5 3 69
93 21 25 53 20
and then count how many numbers are in the file and find the max. I wrote the following script:
file = open ("input.dat","r")
count = 0
for line in file :
parts = line.split()
count +=1
print (count)
But it only counts the number of lines. How do I count the number of numbers? In the file?

You said that you do not want to use lists. However, split() method acts on a string and converts it to a list. I'd focus my answer on the two requirements you specifically mentioned. If there are other requirements, please mention them in the comments below.
Counting the numbers
To do this, you can simply read each line, use split to break it into a list of numbers, and then add the length of that string to a running total.
Here's the code for the same:
file = open ("input.dat","r")
total_numbers = 0
for line in file :
parts = line.split()
total_numbers += len(parts)
print (total_numbers)
Finding the maximum number
As for finding the maximum number is present, for each line you can find the maximum number using inbuilt max function and select the maximum number out of the maximum numbers for each line.
This is how it can be achieved.
file = open ("input.dat","r")
total_numbers = 0
# Storing the most minimum number possible inside max_num
# '-inf' is a special string which implies tending to negative infinity
max_num = float('-inf')
for line in file :
parts = line.split()
# split() stores the numbers as a string inside the list
# map() converts the list of strings into a list of integers
parts = map(int, parts)
max_num_in_line = max(parts)
# If the maximum number in the line being read is more than max_num, replace max_num by that number
if max_num_in_line > max_num:
max_num = max_num_in_line
print (max_num)
I hope this helps. Do let me know if you need further clarification.

You can do everything you want easily using lists:
with open ("input.dat","r") as file:
data = file.read()
#Extract all integers in the file into 1 list
list_of_integers = [int(i) for i in x.replace('\n',' ').split()]
#Now you can whatever you please
count(list_of_integers)
max(list_of_integers)
sum(list_of_integers)
...

If you don't want to use list in your solution you can try this to calculate maximum number and count of all numbers
file = open ("input.dat","r")
count = 0
max = 0
for line in file :
#Reading file will result in list of numbers like ['1', '2', '3'], so you have to map them to int
parts = list(map(int, line.split()))
#Count of all numbers in a line
count = count + len(parts)
#Max number in a lint
max_iter = max(parts)
#Update max if previous max was smaller
if max_iter > max:
max = max_iter
print ("Total Numbers: {}, Maximum Number: {}".format(count,max))

file = open ("input.dat","r")
sum = 0
for line in file :
parts = line.split()
sum+=len(parts)
# Print the total number in a line
print (sum)
parts = map(int, parts)
max_in_line = max(parts)
if(max<max_in_line)
max = max_in_line
print(sum)
print(max)

import re
digit_count=0
number_count = 0
numbers = []
count=0
with open ("letters_and_numbers.txt") as f:
for line in f.readlines():
sub_strs = line.rstrip().split("-")
for i in range(0, len(sub_strs)):
file_words = re.split(r"[a-zA-Z\W]",sub_strs[i])
for word in file_words:
if word.isdigit():
digit_count += len(word)
number_count += 1
if i >=1 and sub_strs[i].startswith(word):
count-=int(word)
digit_count+=1
numbers.append("-")
numbers.append(word)
else:
count+=int(word)
numbers.append(word)
print "digits:",digit_count
print "amount of numbers:",number_count
print "numbers:",numbers
print "total:",count
This code counts the numbers in a file even if the file looks like this:
gfwgf8943758834y34ty3yttubf37t73

Related

My code doesn't work for more then 2 digits numbers and and negative numbers

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

adding the numbers i read from a file

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)

How to extract all the numbers from a text file using re.findall() and compute the sum using a for-loop?

The basic outline of this problem is to read the file, look for integers using the re.findall(), looking for regular expression of [0-9]+ and then converting the extracted strings to integers and summing up the integers. I'm having different outcome it supposed to end with (209). Also, how can I simplify my code? Thanks (here is the txt file http://py4e-data.dr-chuck.net/regex_sum_167791.txt)
import re
hand = open("regex_sum_167791.txt")
total = 0
count = 0
for line in hand:
count = count+1
line = line.rstrip()
x = re.findall("[0-9]+", line)
if len(x)!= 1 : continue
num = int(x[0])
total = num + total
print(total)
Assuming that you need to sum all the numbers in your txt:
total = 0
with open("regex_sum_167791.txt") as f:
for line in f:
total += sum(map(int, re.findall("\d+", line)))
print(total)
# 417209
Logics
To start with, try using with when you do open so that once any job is done, open is closed.
Following lines are removed as they seemed redundant:
count = count+1: Not used.
line = line.rstrip(): re.findall takes care of extraction, so you don't have to worry about stripping lines.
if len(x)!= 1 : continue: Seems like you wanted to skip the line with no digits. But since sum(map(int, re.findall("\d+", line))) returns zero in such case, this is also unnecessary.
num = int(x[0]): Finally, this effectively grabs only one digit from the line. In case of two or more digits found in a single line, this won't serve the original purpose. And since int cannot be directly applied to iterables, I used map(int, ...).
You were almost there:
import re
hand = open("regex_sum_167791.txt")
total = 0
for line in hand:
count = count+1
line = line.rstrip()
x = re.findall("[0-9]+", line)
for i in x:
total += int(i)
print(total)
Answer: 417209

How to find largest number in file and see on which line it is

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

Reading a file and using a loop to output the integers while displaying the largest number python

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

Categories