issue in handling CSV data

Joel Goldstick joel.goldstick at gmail.com
Sat Sep 7 20:28:11 EDT 2019


On Sat, Sep 7, 2019 at 8:21 PM Sharan Basappa <sharan.basappa at gmail.com> wrote:
>
> I am trying to read a log file that is in CSV format.
>
> The code snippet is below:
>
> ###############################
> import matplotlib.pyplot as plt
> import seaborn as sns; sns.set()
> import numpy as np
> import pandas as pd
> import os
> import csv
> from numpy import genfromtxt
>
> # read the CSV and get into X array
> os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\constraints')
> X = []
> #with open("constraints.csv", 'rb') as csvfile:
> #    reader = csv.reader(csvfile)
> #    data_as_list = list(reader)
> #myarray = np.asarray(data_as_list)
>
> my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None)
> print (my_data)
>
> my_data_1 = np.delete(my_data, 0, axis=1)
> print (my_data_1)
>
> my_data_2 = np.delete(my_data_1, 0, axis=1)
> print (my_data_2)
>
> my_data_3 = my_data_2.astype(np.float)
> ################################
>
> Here is how print (my_data_2) looks like:
> ##############################
> [['"\t"81' '"\t5c']
>  ['"\t"04' '"\t11']
>  ['"\t"e1' '"\t17']
>  ['"\t"6a' '"\t6c']
>  ['"\t"53' '"\t69']
>  ['"\t"98' '"\t87']
>  ['"\t"5c' '"\t4b']
> ##############################
>
> Finally, I am trying to get rid of the strings and get array of numbers using Numpy's astype function. At this stage, I get an error.
>
> This is the error:
> my_data_3 = my_data_2.astype(np.float)
> could not convert string to float: " "81
>
> As you can see, the string "\t"81 is causing the error.
> It seems to be due to char "\t".
>
> I don't know how to resolve this.
>
> Thanks for your help.
>
> --
> https://mail.python.org/mailman/listinfo/python-list

how about (strip(my_data_2).astype(np.float))

I haven't used numpy, but if your theory is correct, this will clean
up the string


-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays



More information about the Python-list mailing list