1

I would like to convert my csv file which looks like this:

x,y
0,1
1,2
2,3
3,0
3,3

To a 2d array in python, which should look like this:

E = [[0, 1], [1, 2], [2, 3], [3, 0], [3, 3]]

I know that something like this should do:

x = list(csv.reader(open('edges.csv')))

but this add x and y and the items are string [['x', 'y'], ['0', '1'], ['1', '2'], ['2', '3'], ['3', '0'], ['3', '3']]

user346206
  • 165
  • 10
  • Does this answer your question? [How to skip the headers when processing a csv file using Python?](https://stackoverflow.com/questions/14257373/how-to-skip-the-headers-when-processing-a-csv-file-using-python) – zvi Oct 10 '22 at 17:48
  • of course. why did you expect that to work differently? – juanpa.arrivillaga Oct 10 '22 at 18:09

3 Answers3

1

Since you want to do a 1 liner, you read the whole csv file and just dump all the data into the variable. It includes the header as an item in the list.

Also if you want to convert all the strings of integers to integers, there should only be integers in the array for the next part to work properly

So you just need to remove the first item by doing this,

x = x[1:]

or this.

del(x[0])

You can also use the pop() method. There are many ways to remove the first item.

The output for x is now:

[['0', '1'], ['1', '2'], ['2', '3'], ['3', '0'], ['3', '3']]

Next you can do this to convert all the string integers to integers. This will only work properly if there are no characters in the list. It's called a list comprehension.

x = ([list( map(int,i)) for i in x])

The output for x now is,

[[0, 1], [1, 2], [2, 3], [3, 0], [3, 3]]

If you use a loop to go through the csv file you can do something like this, next(reader, None) to skip the header.

If you want the long version, you can do this,

x = []
import csv
with open("edges.csv", "rb") as files:
   reader = csv.reader(files)
   next(reader, None) #this will skip the header (x,y)
   for data in reader:
      x.append(list(map(int,data))) #this line converts each pair of strings into a pair of ints and appends it to the x variable

The output for x will also be.

[[0, 1], [1, 2], [2, 3], [3, 0], [3, 3]]
anarchy
  • 3,709
  • 2
  • 16
  • 48
  • But is there a posibility that the items in this array would be a int instead of strings? – user346206 Oct 10 '22 at 17:29
  • 1
    you are reading a csv file, they will all be strings by default, even the numbers will be parsed as strings, you need to manually convert them to integers, i just realised what your question is trying to do give me a sec ill change my code – anarchy Oct 10 '22 at 17:30
  • @user346206 if i answered your question please mark my answer with a tick – anarchy Oct 10 '22 at 17:41
1

Use csv.reader() but when you're adding new objects to the list change the type of them to int like the below code snippet:

import csv

bidimensional_array = []
with open('edges.csv', newline='\n') as f:
    reader = csv.reader(f, delimiter=',')
    next(reader) # this will skip that header
    for row in reader:
        bidimensional_array.append([int(x) for x in row])

print(bidimensional_array)

My source for more information on csv and help me to answer this question: Skip the header of a file with Python's CSV reader

Javad
  • 2,033
  • 3
  • 13
  • 23
0

You can also use DictReader and list comprehension:

from csv import DictReader
E = [[int(i['x']), int(i['y'])] for i in DictReader(open('edges.csv'))]
print(E)

# [[0, 1], [1, 2], [2, 3], [3, 0], [3, 3]]

Or:

from csv import DictReader
E = list(map(lambda i: [int(i['x']), int(i['y'])], DictReader(open('edges.csv'))))
print(E)

# [[0, 1], [1, 2], [2, 3], [3, 0], [3, 3]]
Arifa Chan
  • 947
  • 2
  • 6
  • 23