-4

I have a csv file with columns containing pixel values as shown in the image. I want to read/access each value, I tried using numpy.genfromtxt but cudn't access the values.

csv file snapshot

enter image description here

Ilya Yaremchuk
  • 2,007
  • 2
  • 19
  • 36
Imaad Qadri
  • 7
  • 1
  • 3

1 Answers1

0

It depends how you want to access them, the following shows how it could be done using Python's CSV library:

import csv

with open('corners_topsingle.csv', 'rb') as f_input:
    csv_input = csv.reader(f_input)
    data = []

    for row in csv_input:
        values = []
        for value in row[2:]:
            x, y = value.strip('()').split(',')
            values.append((int(x), int(y)))

        data.append(row[:2] + values)

print data

This would give you data looking like:

[['1', '02.jpg', (86, 54), (89, 454), (321, 76), (309, 418)], 
 ['2', '03.jpg', (86, 54), (89, 454), (321, 76), (309, 418)], 
 ['3', '04.jpg', (86, 54), (89, 454), (321, 76), (309, 418)]]

This assumes you are using Python 2.x and a row of your CSV file looks something like:

1,02.jpg,"(86,54)","(89,454)","(321,76)","(309,418)"

For Python 3.x, use:

with open('corners_topsingle.csv', 'r', newline='') as f_input:

and change to print(data)

Martin Evans
  • 45,791
  • 17
  • 81
  • 97