L.S
L.S

Reputation: 13

excel.csv file rounding issue when importing to Python

I have written the following code to import values from a csv file:

directory = os.path.join("fileLocation") 
file = 'fileName.csv'
filename = directory + file
datafile = open(filename,'r')
reader = csv.reader(datafile)
data1 = []
for row in reader:
    data1.append(float(row[0]))
resistance_values = np.array(data1)

the .csv file has values that are structured as follows:

0
19,54091284
31,73614448
...

I would like the data imported in to python as a list of floats such as this:

0.0
19.54091284
31.73614448
...

Currently the values are being rounded to look like this:

0.0
19.0
31.0
...

How can I avoid this rounding issue?

Upvotes: 1

Views: 528

Answers (4)

Lorenzo
Lorenzo

Reputation: 162

There are several small issues affecting your code.

If values are written with a comma for decimal separator, then file is a dialect of CSV, which (as the name Comma-Separated-Values suggests) should use commas to separate different values. You can use the delimiter option in csv.reader to specify this dialect.

Then, as suggested by other guys, apply the replace attribute to row[0] to switch to a dot decimal separator in the string holding your value.

However, I would stick to the 'with' language construct instead of using directly open. And finally, I would use correctly os.path.join to join path and filename (but this depends also on which OS you are developing).

Finally, decide once for all if you want to use " or ' for your strings, it will make your life easier.

This gives at the end:

import os
import csv
import numpy as np

directory = 'fileLocation'
file = 'fileName.csv'
filename = os.path.join(directory, file)
datafile = open(filename, 'r')

with open(filename , 'r') as datafile:
    reader = csv.reader(datafile, delimiter =';')
    data1 = []
    for row in reader:
        item = row[0].replace(',', '.')
        data1.append(float(item))

resistance_values = np.array(data1)

Upvotes: 0

Theo
Theo

Reputation: 61168

I think this may help you:

locale.setlocale(LC_NUMERIC, 'nl-NL') # set to a locale where the comma is used as decimal point

for row in reader:
    data1.append(float(locale.atof(row[0])))
resistance_values = np.array(data1)

instead of float() you could also try decimal() If float() still does too much rounding.

Upvotes: 1

The problem is that you are using a European format for your number and the Comma Separated Variable CSV reader uses a comma as a column separator by default, so you'll find that the string value of row[0] is actually "19", the string value of row[1] is "54091284", whereas you are expecting row[0] to be "19,54091284". You can solve this in a couple of ways, but I would solve it simply by converting whole rows of the file into non-European format with a dot before parsing.

with open(filename,'r') as f:
    data1 = [float(line.replace(',', '.')) for line in f.readlines()]

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71610

Try:

import numpy as np
datafile = open(filename,'r').read()
resistance_values = np.array([float(i.replace(',','.')) for i in datafile.splitlines()])
print(resistance_values)

Upvotes: 1

Related Questions