SDG
SDG

Reputation: 2342

Pythonic way of creating a list of list of floats from text file

I am reading from a csv_reader, line by line and trying to convert the list of strings of floats on the same line. Currently, I have:

list([float(i) for i in map(list, csv_reader)])

This clearly does not work. How would I achieve what I want? I want this all to be on a single line too.

Would I require two map functions? Maybe two Pythonic for loops?

The function that I am dealing with is:

def csv_input(filename):

    print(f'Currently reading the annotations from {filename}')

    try:
        csv_input_file = open(filename, 'rt')
    except FileNotFoundError:
        program_help()
        print("[Error] Input File not found")

    csv_reader = csv.reader(csv_input_file, delimiter=',')
    unfiltered_annots = list(float(i) for i in map(list, csv_reader))
    csv_input_file.close()

    return unfiltered_annots

My CSV file looks like this:

11, 11, 24, 24, 0.75
10, 11, 20, 20, 0.8
11, 9, 24, 24, 0.7
40, 42, 20, 20, 0.6

And the error I am getting is:

Traceback (most recent call last):
  File "maximal_supression.py", line 124, in test_google_doc_example
    unfiltered_annots = csv_input('example_input.csv')
  File "maximal_supression.py", line 34, in csv_input
    unfiltered_annots = list(float(i) for i in map(list, csv_reader))
  File "maximal_supression.py", line 34, in <genexpr>
    unfiltered_annots = list(float(i) for i in map(list, csv_reader))
TypeError: float() argument must be a string or a number, not 'list'

Upvotes: 0

Views: 89

Answers (1)

vurmux
vurmux

Reputation: 10020

You are trying to convert list to float. If you want to convert elements of list to floats, you should additionally iterate through your lists inside your list comprehension:

unfiltered_annots = list([[float(i) for i in l] for l in map(list, csv_reader)])

In my slightly converted code (for simplicity):

import csv

csv_input_file = open('a.csv', 'rt')
csv_reader = csv.reader(csv_input_file, delimiter=',')
unfiltered_annots = list([[float(i) for i in l] for l in map(list, csv_reader)])
csv_input_file.close()
unfiltered_annots

It returns the list of lists:

[[11.0, 11.0, 24.0, 24.0, 0.75],
 [10.0, 11.0, 20.0, 20.0, 0.8],
 [11.0, 9.0, 24.0, 24.0, 0.7],
 [40.0, 42.0, 20.0, 20.0, 0.6]]

P.S. As @meowgoesthedog mentioned, csv_reader returns lists so you don't need to map a list to csv_reader:

unfiltered_annots = [list(map(float, l)) for l in csv_reader]

Upvotes: 1

Related Questions