我正在定义一个函数,它将返回列表,其中元素零是2D阵列,元素之一是标题信息,元素2是rowname。我怎样才能从文件中读取这其中创建一个函数以允许标题行和列名称列
文件看起来像这样:
基因S1,S2,S3 S4 S5
100 -0.243 -0.021 -0.205 -1.283 0.411
10000 - 1.178 -0.79 0.063 -0.878 0.011
def input2DarrayData(fn):
# define twoDarray, headerLine and rowLabels
twoDarray = []
# open filehandle
fh = open(fileName)
# collect header information
# read in the rest of the data and organize it into a list of lists
for line in fh:
# split line into columns and append to array
arrayCols = line.strip().split('\t')
# collect rowname information
**what goes here?**
# convenient float conversion for each element in the list using the
# map function. note that this assumes each element is a number and can
# be cast as a float. see floatizeData(), which gives the explicit
# example of how the map function works conceptually.
twoDarray.append(map(float, arrayCols))
# return data
return twoDarray
我不断收到一个错误说,它不能转换为一个浮动的第一个字的文件(基因),因为它是一个字符串。所以,我的问题是搞清楚如何在阅读只是一线
我们可以看到源文件前3行的例子吗?我_think_建议第一行包含列标题,后续行包含数字数据。它是否正确? – 2011-05-08 22:42:31
请说明arrayCols的内容,即,将其打印 – joaquin 2011-05-08 22:43:29
基因\t \t S1 S2 S3 \t \t \t S4 S5 \t -0.243 -0.021 -0.205 \t \t \t -1.283 0.411 \t -1.178 -0.79 0.063 \t \t -0.878 \t 0.011 – Eugene 2011-05-08 22:48:28