reading data file into a list

  • Thread starter ArifulHossain tuhin
  • Start date
A

ArifulHossain tuhin

I have this data file which contains raw data in newline terminated for like below:

10.6626
11.2683
11.9244
12.5758
14.1402
15.1636

Now i want to read that file and import this data into a numpy array. how should i go about it?
 
S

Steven D'Aprano

I have this data file which contains raw data in newline terminated for
like below:

10.6626
11.2683
11.9244
12.5758
14.1402
15.1636

Now i want to read that file and import this data into a numpy array.
how should i go about it?

That depends. How big is the file? Could it include extraneous whitespace
and possibly even comments? How do you expect to deal with bad data? What
version of Python are you using? Who will be maintaining this, a Python
expert or somebody who doesn't know Python very well?

Here's one way (untested but should work):


import numpy
f = open("my data file.txt", "r")
contents = numpy.array([float(line) for line in f])
f.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top