Problems With Reading In Csv Values Into 2d Numpy Array
I am having issues reading in values from a saved csv file. This is part of the csv file that I have: 000000216739.jpg, 224, [ 0. 0. 0. 0. 36. 44. 4. 0. 0. 0. 0. 0. 0.
Solution 1:
I presume your data is like this; filename,nrows,[imrow1],[imrow2],[imrow3], ...,[imrow_nrows] then do the following to extract the image from your data. Let us know if your data is formatted differently.
with open(CSVFilepath) as f:
reader = csv.reader(f,delimiter=',')
for row in reader:
fname=row[0]
nrows=int(row[1])
imdata=np.array(row[2:]) #Get from the 2 element to the end. Could also do row[:-nrows]
#might need to make each element an int or float.
###reshape if that's what you need and do something with the image
Solution 2:
Your file is not a correct csv file, and you should not read it like a csv file.
The linebreak in csv file represent a new row whereas it's apparent that in your file they don't mean that - you want to read the numbers inside [
and ]
but they're not delimited properly.
A way to dissect this file would be to
with open(file,'r') as fin:
f = fin.readlines()
f = ' '.join(f) # remove newlines
listrows = f.split('[')
listrows = [l.split(']')[0] for l in listrows] # Get string between '[' and ']'
matrix = [row.split('.') for row in listrows] # This is now a 2D matrix
final = [[int(e.replace(' ','')) for e in row] for row in matrix] # Here goes your final matrix
I used list comprehension extensively so this does not go to 30 lines. Try running this.
Post a Comment for "Problems With Reading In Csv Values Into 2d Numpy Array"