简体   繁体   中英

how to import data from a text file into pandas dataframe

I want to read a text file as a pd dataframe , but i don't know which delimiter should i choose to read the data correctly.

Data in the text file looks like this:

在此处输入图像描述


USAF   WBAN  STATIONNAME   CTRY ST CALL  LAT     LON      ELEV(M) BEGIN    END

007018 99999 WXPOD 7018                  +00.000 +000.000 +7018.0 20110309 20130730

I need to have "STATIONNAME" in one column but what i choose delimiter = ' ' , data in that column will be separated each word in a column and all the data in the file will be messed up.

Any suggestion please !

We can't tell for sure from the picture, but the data may be tab-delimited. If so, you can read the file like this:

df = pd.read_csv('<filename>', sep='\t')

Thank you, I solved the problem using pandas.read_fwf(path,compression='infer') . This command is used to read fixed-width-files.

We can also specify the header using pandas.read_fwf(path,compression='infer',names=header) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM