Skip to content Skip to sidebar Skip to footer

How To Find Minimum Value From *.txt File

I have *.txt file, which contains data like this: ... 30 5.882973099631601 31 6.035463584639685 32 6.184276444600495 33 6.3336851616329435 34 6.492617147379082 35 6.683538372963013

Solution 1:

import csv

# preparing data - converting to array
rows = []
withopen('sample.txt', mode='r') as infile:
    reader = csv.reader(infile, delimiter=" ")
    for row in reader:  # each row is a list
        rows.append(row)

# lambda function to filter min considering the second column
minimus = min(rows, key=lambda x: float(x[1]))

# doneprint(minimus)

Solution 2:

For getting the row with minimum value:

min_row = ''  
min_val = 1000000# Considering this value to be the highest'''
Loop for reading rows in source file
'''
a, b = row.split(' ')
b = float(b)
if b < min_val:
    min_val = b
    min_row = row
'''
End of loop
'''

Then, write 'min_row' in the new file.

Post a Comment for "How To Find Minimum Value From *.txt File"