Error in CSV file, Python

0

I want to read a file on my PC but I get an error, maybe you can help me

import numpy as np
import csv
import logging
csvFile = 'C:/Users/Usuario/Desktop/house/GFG.csv'

def __init__(self, _data=csvFile, _shortTerm=10, _longTerm=15):
        self.closingPrices = []

        with open(csvFile,'r') as CSVFile:
            reader = csv.reader(CSVFile, delimiter=';')
            for row in reader:
                self.closingPrices.append(row[4] # Tomo los datos de cierre
        self.closingPrices.pop(0)    # Eliminio primer fila de información 
        self.closingPrices = map(float, self.closingPrices)
        self.data =[]
        self.shortTerm = _shortTerm
        self.longTerm = _longTerm
        self.shortAverage = self.setShortAverage()
        self.longAverage = self.setLongAverage()
        self.lastIndicator = None
        self.init_logging()

The error is as follows:

  

ValueError: could not convert string to float: '116.85'

    
asked by Napoleon Ricaurte 12.06.2018 в 22:52
source

1 answer

0

And why do not you read it with pandas?

import pandas as pd

df = pd.read_csv(
    filepath_or_buffer='C:/Users/Usuario/Desktop/house/GFG.csv',
    sep=';',
)

This way you can work much better your csv, for example in the part you do the self.closingPrices.pop(0) , with a dataframe would be.

df.drop(labels=0, axis=1) # axis=1 indica columnas

You can find the documentation in:

link

in this link as well

Greetings.

    
answered by 14.06.2018 в 23:42