KarlHuang
KarlHuang

Reputation: 121

How can I read this ASCII data without column name assigned using Python?

Please see the attached data file for its format, there are 6 header lines showing the information of the data matrix, in this case, 100 rows and 100 columns. I try to use the following codes:

path = "ArcGIS.txt" data = pd.read_csv(path, sep=" ", header = 6, index_col = False, na_values = -9999)

But the data I get is not 100 rows and 100 columns. I don't know how to upload attachment, so I just use an image to show the format (data is delimited by 'space'):

data capture

Upvotes: 1

Views: 369

Answers (1)

jacoblaw
jacoblaw

Reputation: 1283

data = pd.read_csv(path, sep=" ", header = 5, names=[i for i in range(100)], na_values = -9999)

It should ignore the first 6 lines (header=5 for that), and then give columns names from 0 to 99

And you mentioned you wanted the header as well. I cant think of a way to do it with pandas, but here is a handy way:

path = "ArcGIS.txt"
params = dict()
with open(path, 'r') as f:
    for i in range(6):
        key, val = f.readline().split()
        params[key] = val

This will give you a dict with all of those parameters. Hope this works for you.

Upvotes: 1

Related Questions