Home > Back-end >  How to get a number greater than a threshold out of a string inside csv
How to get a number greater than a threshold out of a string inside csv

Time:10-16

I have previously written a code that just checks if a certain letter is inside the second row of a csv file then prints the first row corresponding to it, but now I would need it to get check if the highest number is above 0.80 as well as what letters are in the "BestLetter" section. Here is my current code:

import csv
import os

with open('test.csv', 'r') as f:
  reader = csv.reader(f, delimiter=',')
for row in reader:
  if "ar" in row[1]: # this takes the first instance of a standalone letter
    print(row[0])

and now I was trying something like this:

for n in row[1]:
    if n.isdigit():
        print(n.isdigit())

but I am unsure if my logic is correct as it does not seem to be working. Also below is a sample of what is inside the .csv file that I have to work with

#foo,["BestLetter":"B","allLetters":{"A":0.5,"B":90.0,"C":0.0,"D":0.0,"E":0.0,"F":0.0,"G":1.4,"H":0.0,"I":0.0,"J":0.0,"K:7.3,"L":0.7}]

in this case B is above 0.80

Any help would be appreciated!

CodePudding user response:

If I'm understanding your problem correctly, you could just convert the entry to a float to compare it to 0.8. Put it inside of a try except inside your loop and just continue in the except block

for n in row[1]:
    try:
        num = float(n)
        if num > 0.8:
            print(n)
    except:
        continue

isdigit() won't work here because it returns false when there is a '.' in the string.

CodePudding user response:

With a small change in the input data:

#foo|{"BestLetter":"B","allLetters":{"A":0.5,"B":90.0,"C":0.0,"D":0.0,"E":0.0,"F":0.0,"G":1.4,"H":0.0,"I":0.0,"J":0.0,"K":7.3,"L":0.7}}

Use | as delimiter and change the second item to a JSON object string. Also correct "K:7.3 to "K":7.3. Save above in file with name number_test.csv

Then:

import csv
import json

with open("number_test.csv") as csv_f:
    csv_reader = csv.reader(csv_f, delimiter='|')
    letter_dict = {}
    for row in csv_reader:
        j = json.loads(row[1])
        best_letter = j["BestLetter"]
        for letter, score in j["allLetters"].items():
            if score  > 0.80:
                letter_dict.update({letter: score})
            letters_sorted = sorted(letter_dict.items(), key=lambda item: item[1], reverse=True)

best_letter
'B'

letters_sorted
[('B', 90.0), ('K', 7.3), ('G', 1.4)]

top_letter = letters_sorted[0]
('B', 90.0)




  • Related