0

I am importing numbers from a csv file that looks like the picture attached: CSV File View

However, those numbers are imported as a string and not a float so I need to convert. I've tried using this method but it just says it cannot convert.

import csv
import numpy as np

with open('20191031-0002_09.csv', mode='r') as infile:
    reader = csv.reader(infile)
    next(reader, None)
    next(reader, None)
    next(reader, None)
    y = [columns[1] for columns in reader]

From research on stack I found this which I thought might work:

numbers = []
for x in y:
      numbers.extend([n for n in map(float, line.split(' '))])

However, my array still comes out as a list of strings like this:

['1.09805600', '1.09805600']

Whereas, I want it to be a array of floats.

Hope you can help.

Ben Watson
  • 29
  • 5
  • 1
    `line.split()` where `line` is declare??? – Divyesh patel Nov 01 '19 at 10:16
  • Since it appears you're trying to do something with Numpy, try using `np.genfromtxt` to read the CSV file instead, as in this answer https://stackoverflow.com/questions/3518778/how-do-i-read-csv-data-into-a-record-array-in-numpy Alternatively, use Pandas. – Iguananaut Nov 01 '19 at 10:55

3 Answers3

0

This is quite confusing, as pointed out above you have not declared line anywhere, but if you want to convert the string '1.09805600' to a float, simply call it a float.

>>> x = '1.09805600'
>>> float(x)
1.09805600
Jamie J
  • 1,168
  • 1
  • 9
  • 22
0

please try below:

import csv
import numpy as np

with open('20191031-0002_09.csv', mode='r') as infile:
    reader = csv.reader(infile)
    y = [columns[1] for columns in reader]
    print (y)
numbers = []
numbers.extend([n for n in map(float,y)])
print (numbers)

Demo

if it's not work then please add your sample csv file in given demo link

Divyesh patel
  • 967
  • 1
  • 6
  • 21
0

If you want to convert both columns to float so that you end up with a list that looks like:

[
    [0, 1.098056],
    [.000002, 1.098056],
    etc.
]

Then this will do it:

import csv

with open('20191031-0002_09.csv', mode='r') as infile:
    reader = csv.reader(infile)
    next(reader, None)
    next(reader, None)
    next(reader, None)
    rows = [[float(columns[0]), float(columns[1])] for columns in reader]

If you just care about the second column:

    y = [float(columns[1]) for columns in reader]

Edited to handle columns with bad values:

import csv, sys

with open('20191031-0002_09.csv', mode='r') as infile:
    reader = csv.reader(infile)
    next(reader, None)
    next(reader, None)
    next(reader, None)
    y = []
    for columns in reader:
        try:
            y.append(float(columns[1]))
        except ValueError:
            print(f"Unable to convert '{columns[1]}'", file=sys.stderr)
            # uncomment out the next line if you want to substitute 0.0
            # y.append(0.0)
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • Hi, thanks for your help but it still says: ValueError: could not convert string to float: '∞' – Ben Watson Nov 01 '19 at 15:19
  • @BenWatson Then you have in one of the columns something that is not a valid string that can be converted. What would like to do with a value such as `'∞'`? Bypass it altogether? Use a value of 0? By the way, was this in the first column or are you really only interested in converting the second column. There is a fix for everything. – Booboo Nov 01 '19 at 15:55