1

Here is how the .dat file looks like when I open it in the sublime editor. How can I read specific columns in that ".dat file" without having a header and then store those column values comma separated in a list?

   01 04 16 57 15 30 00 119.8 -24.5 38.5
   02 04 20 03 17 25 00 112.2 -29.1 38.4
   03 04 25 42 19 04 00 113.5 -37.2 38.6
   04 05 00 06 21 31 00  70.6 -41.4 42.2
   05 02 55 13 20 28 00 248.8 -29.8 28.5
   06 03 50 18 17 10 00 148.5 -29.8 35.0
   07 04 31 00 14 44 00 106.4 -26.1 37.5
   08 04 48 27 18 45 00  84.3 -34.5 38.5
   09 04 15 25 21 27 00 101.4 -36.6 35.4
   10 04 23 19 22 42 00 116.2 -45.8 35.1
   11 04 26 02 12 56 00 107.4 -15.6 33.4
   12 03 56 56 10 11 00 141.9  -6.2 39.6
   13 04 12 56 15 16 00 117.4 -24.5 36.9
   14 04 17 09 13 54 00 121.6 -18.2 42.0
   15 04 17 46 14 58 00 113.1 -20.2 36.2
   16 04 18 04 13 44 00 109.2 -20.7 37.0
   17 04 21 13 17 19 00 113.4 -31.8 37.5
   18 04 22 02 18 55 00 112.5 -34.5 36.6
   19 04 22 23 22 10 00 113.5 -40.7 40.1
   20 04 22 26 22 05 00 119.7 -44.2 32.0
   21 04 22 36 17 48 00 115.8 -36.4 34.7
   22 04 22 46 15 49 00 105.0 -25.3 36.4
   23 04 25 02 21 30 00 105.9 -41.3 36.2
   24 04 25 43 15 51 00 109.6 -29.3 40.0
   25 04 25 48 15 45 00 111.7 -25.5 39.5
   26 04 27 42 16 05 00 109.2 -23.9 37.5
   27 04 27 48 15 35 00 107.2 -24.7 39.3
   28 04 27 48 13 37 00 113.5 -19.3 38.8
   29 04 29 00 15 44 00 107.5 -29.5 36.0
   30 04 34 41 15 02 00 109.5 -29.2 40.2
   31 04 35 17 15 56 00  93.4 -28.6 38.4
   32 04 35 51 23 03 00 111.3 -57.8 40.0
   33 04 41 39 11 03 00  98.4 -11.9 39.4
   34 04 56 52 15 50 00  87.6 -32.2 44.1

Later, I want to use this list to convert the right ascension and declination from hour, minutes, seconds, and, respectively degree minutes and so on...to degree decimals for further calculations.

I have been struggling a different way through online help, but I am unable to go any further.
Any help would be appreciated. I am totally beginner.

zx485
  • 28,498
  • 28
  • 50
  • 59
FRQ
  • 29
  • 1
  • 11
  • 2
    Possible duplicate of [Convert a space delimited file to comma separated values file in python](https://stackoverflow.com/questions/19759423/convert-a-space-delimited-file-to-comma-separated-values-file-in-python) – r.ook Nov 12 '18 at 16:55
  • That doesn't help me in my case – FRQ Nov 12 '18 at 17:08

2 Answers2

2

use the pandas read_csv, but specify delimiter as whitespace. Then specify column names. See mockup below:

import csv
df = pd.read_csv("file.dat", delim_whitespace=True, names=['a','b','c','d','e','f','g','h','i','j'], )
df

Result below:

enter image description here

MEdwin
  • 2,940
  • 1
  • 14
  • 27
  • 2ndly I imported io and StringIO but then and by running this code f = io.stringIO("fakepath\Exercises\hyades_pm.dat") it gives me following error: TypeError: initial_value must be unicode or None, not str – FRQ Nov 12 '18 at 18:12
  • okay, I have now modified it. so you dont need io. Just pandas. Let me know if it works. – MEdwin Nov 13 '18 at 09:16
0

Try this

with open("data.dat",'r') as file:
      text = [x for x in file.read().split("\n")]
      file.close()
print(text[0:2])
PZ_R
  • 37
  • 7
  • and then you can access list as you like – PZ_R Nov 12 '18 at 17:12
  • You could just do `text = [line for line in file.readlines()]` instead... or better yet, `text = [file.readline() for _ in range(2)]` instead of `text[0:2]`. You also don't need the `file.close()` as the `with` context manager will handle that for you when you exit the block. However I don't think your answer helps OP. – r.ook Nov 12 '18 at 17:14
  • The 1st code of yours give me this result as follows: [' 01 04 16 57 15 30 00 119.8 -24.5 38.5', ' 02 04 20 03 17 25 00 11 2.2 -29.1 38.4'] but what about the single quote and space in the beginning? – FRQ Nov 12 '18 at 17:24
  • use strip to remove space and its a list you can access any element by passing index number. is it the thing you need? – PZ_R Nov 12 '18 at 17:43
  • what would be in the place of x in for loop or what is "x" in for loop... is it index? plus I want 2nd,3rd and fourth column in one list for example and similarly 5th and 6th in another list... so that I can do a conversion on those list for my angles. – FRQ Nov 12 '18 at 17:45
  • x is place holder/variable which gets output of file being read. Read list comprehension to play with data. – PZ_R Nov 12 '18 at 18:09