2
Given a particular file iris.csv:
"sepal_length,sepal_width,petal_length,petal_width,species"
"5.1,3.5,1.4,0.2,setosa"
"4.9,3,1.4,0.2,setosa"
"4.7,3.2,1.3,0.2,setosa"
"4.6,3.1,1.5,0.2,setosa"
I try to load the file with the following code:
import os
import numpy as np
filename = os.path.join('iris.csv')
arquivo = np.loadtxt(filename, delimiter=',', usecols=(0,1,2,3), skiprows=1)
Returned error: could not Convert string to float: '"5.1'
I try to remove double quotes with the code below, however the error persists:
input_fd = open('iris.csv', 'r')
output_fd = open('saida.csv', 'w')
for line in input_fd.readlines():
line = ','.join(['%s'%field.strip() for field in line.split(';')])+'\n'
output_fd.write(line)
input_fd.close()
output_fd.close()
iris = open('saida.csv', 'r')
So, how can I automate the quotation marks between the lines of the file . csv?
If your file is really asism, with quotes around the whole line, the file is incorrect - no ready function will be able to read the file - answer below.
– jsbueno