0

I have a text file that looks something like this:

TITPLE="SurfaceData"
VARIABLES="X", "Y", "Z", "Cutoff Rigidity", "Injected Particle Number"
ZONE N=30, E=25, DATAPACKING=POINT, ZONETYPE=FEQUADRILATERAL
0.000000e+00 0.000000e+00 6.371000e+06  4.843809e-01  2.001000e+03
0.000000e+00 0.000000e+00 6.371000e+06  4.635282e-01  2.022500e+03
0.000000e+00 0.000000e+00 6.371000e+06  1.158746e+00  1.984500e+03
0.000000e+00 0.000000e+00 6.371000e+06  1.153130e+00  1.960500e+03
0.000000e+00 0.000000e+00 6.371000e+06  4.873921e-01  1.986500e+03
1 2 7 6
2 3 8 7
3 4 9 8
4 5 10 9

In python 3, I would like to convert the first 3 columns of data into 3 separate lists and ignore all of the other columns and data in the file. In other words, I would like to convert the following three columns into three lists:

0.000000e+00 0.000000e+00 6.371000e+06
0.000000e+00 0.000000e+00 6.371000e+06 
0.000000e+00 0.000000e+00 6.371000e+06  
0.000000e+00 0.000000e+00 6.371000e+06  
0.000000e+00 0.000000e+00 6.371000e+06 

where each column represents a separate list. I have seen ways to do this when there is no other data in the file, but I cannot find a way to ignore the text above the data and the set of integers below that data. Does anyone have any solutions?

  • Why don't you read the file, skip the first 3 lines, read the rest of the lines, split each at whitespace into a list, and keep the first 3 values from the list? – mkrieger1 Jul 30 '21 at 19:46

1 Answers1

2

You could use pandas, it also fixes the scientific notation:

import pandas as pd

df = pd.read_csv('filename.txt', delim_whitespace=True, header=None, skiprows=3, skipfooter=4, engine='python')

Output df:

0 1 2 3 4
0 0.0 0.0 6371000.0 0.4843809 2001.0
1 0.0 0.0 6371000.0 0.4635282 2022.5
2 0.0 0.0 6371000.0 1.1587459999999998 1984.5
3 0.0 0.0 6371000.0 1.15313 1960.5
4 0.0 0.0 6371000.0 0.48739209999999994 1986.5

To keep only the first three columns: df = df[[0,1,2]]

To convert a column to a list: df[0].tolist()

RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26