필터 지우기
필터 지우기

Read text file with fixed width columns and mixed data types

조회 수: 2 (최근 30일)
Rod Letchford
Rod Letchford 2019년 2월 20일
댓글: Rod Letchford 2019년 2월 21일
I want to read the data columns similar to the attached file "wds00524-6930.txt". The format of the file is explained fully in the "datarequest_key.txt". Briefly, the data begins on line 12 and each line has 25 data columns making a width of 121 per data line. For example, the first column of data has a format of f10.5 and is from width 008 to 017. Some columns have data types 'double' others 'string'. I have tried all sort of ways to read this but have always resorted to importing the text file into excel and selecting what I want and saving it as a csv or xlsx file . This works well on an individual file basis, but I want to be able to read hundreds-thousands of text files in the same format.
  댓글 수: 1
KSSV
KSSV 2019년 2월 20일
편집: KSSV 2019년 2월 20일
What data you want to read from the file?

댓글을 달려면 로그인하십시오.

답변 (1개)

Rod Letchford
Rod Letchford 2019년 2월 20일
KSSV, the data always begins on line 12 (Notepad++ counting) with the first date, in this case being '1834.84'. The last line - in this particular case - is line 62.
  댓글 수: 1
Rod Letchford
Rod Letchford 2019년 2월 21일
Apart from not finding the last line correctly, this python code I wrote does what I want Matlab to do:
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
filename = 'wds00524-6930.txt';
colspecs = [(7,17),(18,19),(19,26),(27,33),(34,35),(35,44),(45,46),(46,53),\
(54,55),(55,61),(61,62),(62,67),(68,69),(69,75),(75,76),(76,81),\
(82,90),(90,91),(92,97),(97,98),(99,101),(102,110),(111,113),\
(114,116),(116,117)]
names = ['date','tflag','theta','terr','rflag','rho','reflag','rerr','mflag1',\
'mag1','m1eflag','m1err','mflag2','mag2','m2eflag','m2err','filter',\
'fflag','tel','teflag','nn','ref','tech','codes','d.e. note']
WDS = pd.read_fwf(filename,header=10,colspecs=colspecs,names=names)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by