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]]