How to extract numbers from a text file and multiply them together? - python

I have a text file which contains 800 words with a number in front of each. (Each word and its number is in a new line. It means the file has 800 lines) I have to find the numbers and then multiply them together. Because multiplying a lot of floats equals to zero, I have to use logarithm to prevent the underflow, but I don't know how.
this is the formula:
cNB=argmaxlogP(c )+log P(x | c )
this code doesn't print anything.
output = []
with open('c:/python34/probEjtema.txt', encoding="utf-8") as f:
w, h = map(int, f.readline().split())
tmp = []
for i, line in enumerate(f):
if i == h:
break
tmp.append(map(int, line.split()[:w]))
output.append(tmp)
print(output)
the file language is persian.
a snippet of the file:
فعالان 0.0019398642095053346
محترم 0.03200775945683802
اعتباري 0.002909796314258002
مجموع 0.0038797284190106693
حل 0.016488845780795344
مشابه 0.004849660523763337
مشاوران 0.027158098933074686
مواد 0.005819592628516004
معادل 0.002909796314258002
ولي 0.005819592628516004
ميزان 0.026188166828322017
دبير 0.0019398642095053346
دعوت 0.007759456838021339
اميد 0.002909796314258002

You can use regular expressions to find the first number in each line, e.g.
import re
output = []
with open('c:/python34/probEjtema.txt', encoding="utf-8") as f:
for line in f:
match = re.search(r'\d+.?\d*', line)
if match:
output.append(float(match.group()))
print(output)
re.search(r'\d+.?\d*', line) looks for the first number (integer or float with . in each line.
Here is a nice online regex tester: https://regex101.com/ (for debuging / testing).
/Edit: changed regex to \d+.?\d* to catch integers and float numbers.

If I understood you correctly, you could do something along the lines of:
result = 1
with open('c:/python34/probEjtema.txt', encoding="utf-8") as f:
for line in f:
word, number = line.split() # line.split("\t") if numbers are seperated by tab
result = result * float(number)

This will create an output list with all the numbers.And result will give the final multiplication result.
import math
output = []
result=1
eres=0
with open('c:/python34/probEjtema.txt', encoding="utf-8") as f:
for line in (f):
output.append(line.split()[1])
result *= float((line.split()[1]))
eres += math.log10(float((line.split()[1]))) #result in log base 10
print(output)
print(result)
print eres

Related

why I am getting empty list when I use split()?

I have a textfile as:
-- Generated ]
FILEUNIT
METRIC /
Hello
-- timestep: Jan 01,2017 00:00:00
3*2 344.0392 343.4564 343.7741
343.9302 343.3884 343.7685 0.0000 341.0843
342.2441 342.5899 343.0728 343.4850 342.8882
342.0056 342.0564 341.9619 341.8840 342.0447 /
I have written a code to read the file and remove the words, characters and empty lines, and do some other processes on that and finally return those numbers in the last four lines. I cannot understand how to put all the numbers of the text file properly in a list. Right now the new_line generates a string of those lines with numbers
import string
def expand(chunk):
l = chunk.split("*")
chunk = [str(float(l[1]))] * int(l[0])
return chunk
with open('old_textfile.txt', 'r') as infile1:
for line in infile1:
if set(string.ascii_letters.replace("e","")) & set(line):
continue
chunks = line.split(" ")
#Get rid of newlines
chunks = list(map(lambda chunk: chunk.strip(), chunks))
if "/" in chunks:
chunks.remove("/")
new_chunks = []
for i in range(len(chunks)):
if '*' in chunks[i]:
new_chunks += expand(chunks[i])
else:
new_chunks.append(chunks[i])
new_chunks[len(new_chunks)-1] = new_chunks[len(new_chunks)-1]+"\n"
new_line = " ".join(new_chunks)
when I use the
A = new_line.split()
B = list(map(float, A))
it returns an empty list. Do you have any idea how I can put all these numbers in one single list?
currently, I am writing the new_line as a textfile and reading it again, but it increase my runtime which is not good.
f = open('new_textfile.txt').read()
A = f.split()
B = list(map(float, A))
list_1.extend(B)
There was another solution to use Regex, but it deletes 3*2. I want to process that as 2 2 2
import re
with open('old_textfile.txt', 'r') as infile1:
lines = infile1.read()
nums = re.findall(r'\d+\.\d+', lines)
print(nums)
I'm not quite sure if I entirely understand what you are trying to do, but as I understand it you want to extract all numbers which either are in a decimal form \d+\.\d+ or an integer which is multiplied by another integer using an asterisk, so \d+\*\d+. You want the results all in a list of floats where the decimals are in the list directly and for the integers the second is repeated by the first.
One way to do this would be:
lines = """
-- Generated ]
FILEUNIT
METRIC /
Hello
-- timestep: Jan 01,2017 00:00:00
3*2 344.0392 343.4564 343.7741
343.9302 343.3884 343.7685 0.0000 341.0843
342.2441 342.5899 343.0728 343.4850 342.8882
342.0056 342.0564 341.9619 341.8840 342.0447 /
"""
nums = []
for n in re.findall(r'(\d+\.\d+|\d+\*\d+)', lines):
split_by_ast = n.split("*")
if len(split_by_ast) == 1:
nums += [float(split_by_ast[0])]
else:
nums += [float(split_by_ast[1])] * int(split_by_ast[0])
print(nums)
Which returns:
[2.0, 2.0, 2.0, 344.0392, 343.4564, 343.7741, 343.9302, 343.3884, 343.7685, 0.0, 341.0843, 342.2441, 342.5899, 343.0728, 343.485, 342.8882, 342.0056, 342.0564, 341.9619, 341.884, 342.0447]
The regular expression searches for numbers matching one of the formats (decimal or int*int). Then in case of a decimal it is directly appended to the list, in case of int*int it is parsed to a smaller list repeating the second int by first int times, then the lists are concatenated.

How to put a group of integers in a row in a text file into a list?

I have a text file composed mostly of numbers something like this:
3 011236547892X
9 02321489764 Q
4 031246547873B
I would like to extract each of the following (spaces 5 to 14 (counting from zero)) into a list:
1236547892
321489764
1246547873
(Please note: each "number" is 10 "characters" long - the second row has a space at the end.)
and then perform analysis on the contents of each list.
I have umpteen versions, however I think I am closest with:
with open('k_d_m.txt') as f:
for line in f:
range = line.split()
num_lst = [x for x in range(3,10)]
print(num_lst)
However I have: TypeError: 'list' object is not callable
What is the best way forward?
What I want to do with num_lst is, amongst other things, as follows:
num_lst = list(map(int, str(num)))
print(num_lst)
nth = 2
odd_total = sum(num_lst[0::nth])
even_total = sum(num_lst[1::nth])
print(odd_total)
print(even_total)
if odd_total - even_total == 0 or odd_total - even_total == 11:
print("The number is ok")
else:
print("The number is not ok")
Use a simple slice:
with open('k_d_m.txt') as f:
num_lst = [x[5:15] for x in f]
Response to comment:
with open('k_d_m.txt') as f:
for line in f:
num_lst = list(line[5:15])
print(num_lst)
First of all, you shouldn't name your variable range, because that is already taken for the range() function. You can easily get the 5 to 14th chars of a string using string[5:15]. Try this:
num_lst = []
with open('k_d_m.txt') as f:
for line in f:
num_lst.append(line[5:15])
print(num_lst)

Extracted float values are stored in a list of lists instead of a list of values

I am doing an exercise for finding all the float point values in a text file and computing the average .
I have managed to extract all the necessary values but they are being stored in a list of lists and I don't know how extract them as floats in order to do the calculations .
Here is my code :
import re
fname = input("Enter file name: ")
fhandle = open(fname)
x = []
count = 0
for line in fhandle:
if not line.startswith("X-DSPAM-Confidence:") : continue
s = re.findall(r"[-+]?\d*\.\d+|\d+", line)
x.append(s)
count = count + 1
print(x)
print("Done")
and this is the output of x :
[['0.8475'], ['0.6178'], ['0.6961'], ['0.7565'], ['0.7626'], ['0.7556'], ['0.7002'], ['0.7615'], ['0.7601'], ['0.7605'], ['0.6959'], ['0.7606'], ['0.7559'], ['0.7605'], ['0.6932'], ['0.7558'], ['0.6526'], ['0.6948'], ['0.6528'], ['0.7002'], ['0.7554'], ['0.6956'], ['0.6959'], ['0.7556'], ['0.9846'], ['0.8509'], ['0.9907']]
Done
You can make x a flat list of floats from the start:
# ...
for line in fhandle:
# ...
s = re.findall(r"[-+]?\d*\.\d+|\d+", line)
x.extend(map(float, s))
Note that re.findall returns a list, so we extend x by it while applying float to all the strings in it.

Check if duplicate exists and then append a unique digit to line?

INPUT.TXT looks like this -
pr-ec2_1034
pr-ec2_1023
pr-ec2_1099
I want to write a python script which will read this file & add +1 to the line with highest number and then print that line.
Desired output -
pr-ec2_1100
Right now I am able to add +1 to all lines like -
def increment_digits(string):
return ''.join([x if not x.isdigit() else str((int(x) + 1) % 10) for x in string])
with open('INPUT.txt', 'r') as file:
data = file.read()
print(increment_digits(data))
Output-
pr-ec3_2145
pr-ec3_2134
pr-ec3_2134
but this is not what I want. I want to find the line the with largest ending number in input.txt and add +1 to only to that one line after (last underscore)
pr-ec2_1100 is what I want
Something like this:
with open('input.txt') as f:
lines = [l.strip() for l in f.readlines()]
numbers = [int(l.split('_')[1]) for l in lines]
_max = max(numbers)
result = _max + 1
print('result: pr-ec2_{}'.format(result))
output
pr-ec2_1100

Reason for two similar codes giving different result and different approaches to this task

The question is
def sum_numbers_in_file(filename):
"""
Return the sum of the numbers in the given file (which only contains
integers separated by whitespace).
>>> sum_numbers_in_file("numbers.txt")
19138
"""
this is my first code:
rtotal = 0
myfile = open(filename,"r")
num = myfile.readline()
num_list = []
while num:
number_line = ""
number_line += (num[:-1])
num_list.append(number_line.split(" "))
num = myfile.readline()
for item in num_list:
for item2 in item:
if item2!='':
rtotal+= int(item2)
return rtotal
this is my second code:
f = open(filename)
m = f.readline()
n = sum([sum([int(x) for x in line.split()]) for line in f])
f.close()
return n
however the first one returns 19138 and the second one 18138
numbers.txt contains the following:
1000
15000
2000
1138
Because m = f.readLine() already reads 1 line from f and then you do the operation with the rest of the lines. If you delete that statement the 2 outputs will be the same. (I think :))
I'd say that m = f.readline() in the second snippet skips the first line (which contains 1000), that's why you get a wrong result.
As requested.. another approach to the question:
import re
def sum(filename):
return sum(int(x.group()) for x in re.finditer(r'\d+',open(filename).read()))
As said by answers, you are skipping first line because f.readline(). But a shorter approach would be:
n=sum((int(line[:-1]) for line in open("numbers.txt") if line[0].isnumeric()))

Categories