I have a csv column with some labels in it. I need to convert it to list of lists.
ex:
type1(heading)
string1
string2
string3
string4
converting this directly to,
type1 = [[string1],[string2],[string3],[string4]]
I have a csv column with some labels in it. I need to convert it to list of lists.
ex:
type1(heading)
string1
string2
string3
string4
converting this directly to,
type1 = [[string1],[string2],[string3],[string4]]
It seems python has a native lib to deal with such tasks. Maybe you should take a look at https://docs.python.org/2/library/csv.html
You can try the csv module. For example, given a test.txt file:
header_a,header_b,header_c
1,2,3
4,5,6
7,8,9
You can write:
import csv
with open("test.txt", "r") as f:
reader = csv.reader(f)
headers = list(reader.next())
result = [[list(c) for c in row] for row in reader]
for i, header_name in enumerate(headers):
print header_name, [row[i] for row in result]
The result will be:
header_a [['1'], ['4'], ['7']]
header_b [['2'], ['5'], ['8']]
header_c [['3'], ['6'], ['9']]
The document of csv module is here