我有一个samples.csv文件,这个档案有这样的结构:
354,174,27c,20,5c,287,382,a3
59,359,152,115,19d,15a,143,113
8f,1e6,291,55,b1,3f9,39b,ba
3cf,77,20c,316,164,e2,2cb,3c9
72,171,167,a9,3e5,2dc,34f,191
2ad,8c,1f1,1bd,175,3fd,28,2f5
3b1,11f,ab,8b,282,284,192,1c8
310,24b,240,1fe,20e,251,1d5,305
3f1,14b,381,210,1b4,25f,116,228
ba,175,1c2,342,259,de,359,369
它是一个8 x 1000个样本,我想将这个csv转换为二维数组,以提取以下形式的数据:
[i,j]
354 = [0,0]
174 = [0,1]
没有努皮怎么会这样?
感谢
Python有一个用于解析csv文件的内置模块,称为csv
。以下是官方文档中的一个示例:
>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... for row in spamreader:
... print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
您可以将其存储为列表列表:
rows = []
for row in spamreader:
rows.append(row)
print rows[5][3]
data = file('samples.csv').read()
table = [row.split(',') for row in data.split('n')]
print table[0][0]
将每一行转换为一个列表,然后将它们添加到另一个列表中以获得矩阵:
import csv
matrix = []
with open('somefile.csv') as f:
reader = csv.reader(f, delimiter=',')
matrix.append(list(reader))
print(matrix[0][1]) # Output: 174
您可以使用csv和列表理解:
import csv
with open(ur_file) as f:
reader=csv.reader(f)
data=[row for row in reader]
print data
# [['354', '174', '27c', '20', '5c', '287', '382', 'a3'], ['59', '359', '152', '115', '19d', '15a', '143', '113'], ['8f', '1e6', '291', '55', 'b1', '3f9', '39b', 'ba'], ['3cf', '77', '20c', '316', '164', 'e2', '2cb', '3c9'], ['72', '171', '167', 'a9', '3e5', '2dc', '34f', '191'], ['2ad', '8c', '1f1', '1bd', '175', '3fd', '28', '2f5'], ['3b1', '11f', 'ab', '8b', '282', '284', '192', '1c8'], ['310', '24b', '240', '1fe', '20e', '251', '1d5', '305'], ['3f1', '14b', '381', '210', '1b4', '25f', '116', '228'], ['ba', '175', '1c2', '342', '259', 'de', '359', '369']]
print data[0][0]
# 354
您的问题意味着您希望使用元组data[0,1]
访问数据,而不是使用data[0][1]
语法的多维列表
如果这不是拼写错误,您可以使用一个带有两个元素元组作为关键字的字典:
>>> data={}
>>> data[0,0]='354'
>>> data
{(0, 0): '354'}
>>> data[0,0]
'354'
您读取csv的循环变成:
data={}
with open(ur_file) as f:
reader=csv.reader(f)
for i, line in enumerate(reader):
for j, v in enumerate(line):
data[(i, j)]=v
print data
# {(7, 3): '1fe', (4, 7): '191', (1, 3): '115', (9, 1): '175', (6, 4): '282', (3, 0): '3cf', (8, 0): '3f1', (5, 4): '175', (0, 7): 'a3', (5, 6): '28', (2, 6): '39b', (1, 6): '143', (9, 4): '259', (5, 1): '8c', (3, 7): '3c9', (2, 5): '3f9', (8, 5): '25f', (0, 3): '20', (7, 2): '240', (4, 0): '72', (1, 2): '152', (9, 0): 'ba', (9, 5): 'de', (6, 7): '1c8', (3, 3): '316', (2, 0): '8f', (8, 1): '14b', (7, 6): '1d5', (4, 4): '3e5', (6, 3): '8b', (1, 5): '15a', (3, 6): '2cb', (2, 2): '291', (7, 7): '305', (5, 7): '2f5', (5, 3): '1bd', (4, 1): '171', (1, 1): '359', (9, 7): '369', (2, 7): 'ba', (3, 2): '20c', (0, 0): '354', (6, 6): '192', (5, 0): '2ad', (7, 1): '24b', (4, 5): '2dc', (0, 4): '5c', (5, 5): '3fd', (1, 4): '19d', (6, 0): '3b1', (7, 5): '251', (2, 3): '55', (2, 1): '1e6', (8, 7): '228', (8, 6): '116', (9, 3): '342', (4, 2): '167', (1, 0): '59', (9, 6): '359', (6, 5): '284', (3, 5): 'e2', (0, 1): '174', (8, 3): '210', (7, 0): '310', (4, 6): '34f', (9, 2): '1c2', (5, 2): '1f1', (6, 1): '11f', (3, 1): '77', (8, 2): '381', (0, 2): '27c', (7, 4): '20e', (0, 6): '382', (6, 2): 'ab', (4, 3): 'a9', (1, 7): '113', (0, 5): '287', (3, 4): '164', (2, 4): 'b1', (8, 4): '1b4'}
print data[0,1]
# 174
f = open("file.txt", "r")
array = []
for line in f:
array.append(line.split(','))
f.close()