Julian Das
Julian Das

Reputation: 1381

Converting a list into a numpy array

This code is set up to read two columns of data, and then print the first column into the first numpy array and then the second column into the second numpy array.

def read2coldata(filename):

    import numpy
    b = []
    f = open(filename,"r")
    lines = f.readlines()
    f.close()
    for line in lines:
        a = line.split()
        for i in a:
            b.append(i)
    return (numpy.array(b[::2]),numpy.array(b[1::2]))

However this gives:

(array(['1.5', '8', '16', '17'], dtype='|S3'), array(['4', '5', '6', '6.2'], dtype='|S3'))

How do I get rid of the dtype="|S3" parts to just leave:

(array(["1.5","8","16","17"], array(["4","5","6","6.2"])

Upvotes: 3

Views: 3905

Answers (1)

Félix Cantournet
Félix Cantournet

Reputation: 2001

the dtype="S3" you don't want to "go away". When you print a numpy array it gives you the type of the data in it. it isn't part of the data, it is information about how the data is stored and understood by the program.

In your particular example, you read number, so you probably want to use them afterwards in computation or whatever, in which case you'll want the data to be understood as number (floats in you case).

For the moment they are stored as strings, and that's why you see dtype="S3" which essentially means string type of size 3 or less. (IIRC)

I suggest to you an alternative to your function : numpy.genfromtxt is a function to load data from a txt file into a numpy array.

The documentation is quite good and you'll find it very useful if you spend the 20 minutes to understand the parameters.

array1 = numpy.genfromtxt('path_to_my_file.txt', usecols=0)
array2 = numpy.genfromtxt('path_to_my_file.txt', usecols=1)

This should get you started.

Upvotes: 5

Related Questions