0

I have the following CSV file:

name,A,B,C
name 1,2,8,3
name 2,4,1,5
name 3,3,2,5

I need to separate the lines read in CSV into an array, but If I insert the array index, it returns the column, and I need to separately manipulate the row and column. How did I do?

I need it:

[name,A,B,C]
[name 1,2,8,3]
[name 2,4,1,5]
[name 3,3,2,5]

print(array[0])        ## The result: [name,A,B,C]
print(array[0][1])     ## The result: A

My Code:

with open(csvFileName) as csv_file:

    csv_reader = csv.reader(csv_file)

    for row in csv_reader:

        myList = list(row)
        print(myList)

csv_file.close()

Terminal Result:

['name', 'Points', 'Maney', 'Coin']
['Client 1', '2', '8', '3']
['Client 2', '4', '1', '5']
['Client 3', '3', '2', '5']

Thank You

4
  • myList[1:] = list(map(int, myList[1:])) Commented Jun 8, 2021 at 15:37
  • 1
    I don't understand what you are asking. It seems as if you already get what all the data you want, in the format you want. Is your issue that myList is only a single list containing only the current line instead of a list of lists of all the lines? Commented Jun 8, 2021 at 15:39
  • This list I used was just for example. My CSV has 4 rows with 4 columns. I need to get the value of each row or column using an array. If I need to get a value from row 0 column 1 I type [0][1] Commented Jun 8, 2021 at 16:00
  • Thank You aminrd, MisterMiyagi Commented Jun 8, 2021 at 16:10

1 Answer 1

2

Maybe this is how it should be?

myList = []
import csv
with open(csvFileName) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        myList.append(row)
    csv_file.close()

print(myList[0])
print(myList[0][1])
Sign up to request clarification or add additional context in comments.

Comments