Home > OS >  python reads all of the csv into one column
python reads all of the csv into one column

Time:10-28

I know it might be a very simple and repeated question, but I am quite stuck in reading dat files.

I have a dat file and I tried to read it as CSV. The dat file itself look likes this (I only paste the beginning part of this, the rest is similar to the 18th line):

#Occultation start (UTC): 2020-02-23 00:04:22
#Occultation stop  (UTC): 2020-02-23 00:06:40
#Occultation point latitude (degN):    28.0
#Occultation point longitude (degE):  -17.1
#Center of curvature (m):    11574.705   -3554.708  -13613.902
#Radius of curvature (m):  6369130.294
#Azimuth (degN):    129.789
#Undulation (m):     44.336
#Elevation (m):       0.000
#Processing method:  Wave optics (CT2)
#Background data:    ECMWF forecast 
#Receiver data type: L1caL2p
#Navbit correction:  extern
#Occultation type:   Rising 
#OL/CL mode:         OL->CL
#Alt OL/CL (m):   11884
#alt w.r.t. EGM96 geoid|lat|lon|azimuth|ba_opt|impact_opt|refrac|dry_press|dry_temp|geopotential height|ba_raw|ba_L1|ba_L2|ba_model|snr_L1|snr_L2
          0.000  -99999000.000  -99999000.000  -99999000.000   -0.99999E 08  -99999000.000   -0.99999E 08  -99999000.000  -99999000.000  -99999000.000   -0.99999E 08   -0.99999E 08   -0.99999E 08   -0.99999E 08  -99999000.000  -99999000.000

I tried to get rid of the header and skip the first 17 lines by:

newfile=[]
for line in file[17:]:
    newfile.append(line.strip())
with open('newfile.csv','w') as line:
    for element in newfile:
        line.write(element  "\n")

and then trying to read the csv file:

import pandas as pd
df = pd.read_csv('newfile.csv',sep=',')
print(df.head())

the file will be shown in 1 column and 600 rows! how can I separate the column? I tried different operators like :, ;, '' , but non of the change it

CodePudding user response:

As stated in the Pandas documentation for read_csv, the sep parameter is the CSV delimiter, i.e. the character used to delimit each column. In your case, each column seems to be delimited by tabs or multiple spaces. So, the following should do (untested):

import pandas as pd
df = pd.read_csv('newfile.csv',sep='\s ')
  • Related