2

I'm struggling with getting string values into an array in python. I have a file, about 30k entries long, and each row looks like this:

0R1,Sn=0.3M,Sm=0.7M,Sx=1.5M

I don't need the 0R1 part; all I need is all the Sn values in one array, the Sm values in another, and the Sx in another (of course, I haven't figured out how I'm going to get the numerical values out of the string yet, but I'll think about that later). Right now I'm trying to make an array of strings, I suppose.

Here's my code:

fname = '\\pathname...\\WXT51003.txt'
f1 = open(fname, 'r')

import csv
import numpy
from numpy import zeros
reader = csv.reader(f1)
Max = zeros((29697,1), dtype = numpy.str)
Mean = zeros((29697,1), dtype = numpy.str)
Min = zeros((29697,1), dtype = numpy.str)
for i, row in enumerate(reader):
    Min[i] = row[1]
    Mean[i] = row[2]
    Max[i] = row[3]

f1.close()
print Min[0:10]

The output of the print statement is an array with 'S' in every row. How do I get it to read the entire string, and not just the first character?

4

1 回答 1

3
reader = csv.reader(f1)
rows = list(reader)
cols = zip(*rows)
Min = cols[1]
Mean = cols[2]
Max = cols[3]


# or if you really want numpy.arrays
Min = numpy.array(cols[1]) #dtype will be auto-assigned
Mean = numpy.array(cols[2]) #dtype will be auto-assigned
Max = numpy.array(cols[3]) #dtype will be auto-assigned

我会怎么做......(不要为此使用numpy......至少现在还没有)

如果您需要使用 numpy 则使用“S8”的 dtype 或无论您需要多大的字符串...或使用 dtype.object 或其他...默认情况下它将是 len1 字符串类型...但实际上我根据您的代码片段,这里没有理由使用 numpy

于 2013-07-18T21:08:20.807 回答