我导入了我的CSV文件并将数据转换为数组。现在我想知道,我能做什么才能在阵列中打印特定的值?例如,如果我想要第2行,第2列中的值。 另外,我将如何将两个值一起添加? 感谢。
import csv
import numpy as np
f = open("Test.csv")
csv_f = csv.reader(f)
for row in csv_f:
print np.array(row)
f.close()
答案 0 :(得分:0)
import csv
col_position = 2
row_position = 2
f = open("Test.csv")
csv_f = csv.reader(f)
for count_row, row in enumerate(csv_f):
if count_row == row_position:
print row[col_position]
else:
pass
f.close()
请记住,python python计算从0开始的列表位置,并且您的数组实际上是列表列表。因此,如果你想要位置(2,2)并假设没有标题行,你应该实际请求(1,1),因为(0,0)是第一个列表(行)的第一个元素
编辑以发表评论: 假设您要添加数组的元素a =(0,0)和b =(1,1):
import csv
col_a = 0
row_a = 0
col_b = 1
row_b = 1
f = open("Test.csv")
csv_f = csv.reader(f)
for count_row, row in enumerate(csv_f):
if count_row == row_a:
a = row[col_a]
else if count_row == row_b:
b = row[col_b]
else:
pass
print int(a) + int(b)
f.close()
答案 1 :(得分:0)
要获取数组/文件中的特定值,并将它们加在一起:
import csv
f = open("Test.csv")
csv_f = list(csv.reader(f))
#returns the value in the second row, second column of your file
print csv_f[1][1]
#returns sum of two specific values (in this example, value of second row, second column and value of first row, first column
sum = int(csv_f[1][1]) + int(csv_f[0][0])
print sum