0

「#」を含むヘッダーと、カンマで区切られたデータを含むファイルがあります。すなわち

#blah
#blah
#blah 
1, 2, 3, 4, 5, 6
7, 8, 9,10,11,12
...

# をスキップして、データを配列 (列のヒストグラム) に変換できるようにしたいと考えています。これで読み込めます。

filename = 'input.txt'
listin = []
for line in open(filename,'r'):
    li=line.strip()
    if not li.startswith("#"):
        line = line.partition('#')[0]
        #line = line.rstrip()
        listin.append(line.split(',')) 

data = numpy.asarray(listin)
SubData=data[:,0]

hist,nbins = np.histogram(SubData)

Error:
TypeError: cannot perform reduce with flexible type
4

2 に答える 2

1

You need to convert your data to a numeric type. Your array contains strings.

listin.append([int(token) for token in line.split(',')])

Also, you need to rstrip each line to remove the line break character.

于 2013-02-03T20:04:28.427 に答える