简体   繁体   中英

how do i read a text file in line-by-line using Python when the formatting is specific

how do i read a text file in line-by-line using Python when the formatting is specific? My data is "space delimited and looks like this with spaces between each line. In reality there are no blank lines, nor is there an "end" card:

The_Ark.top                                 0   -37.89541   37.89541    0.00000 449.75055 
8 
0.00000     0.00000     29  -37.59748     0.04690   26  -37.89541   449.75055   26 
-0.19951   449.70273     26   -0.15660     4.48848   29  -34.20844     4.80188   26 
-33.71897   443.53000     26   -0.45357   443.32349   26    0.00000     0.00000    0
{possibly more lines ... to the end}

data on line 1: filename, xMin, xMax, yMin, yMax

data on line 2: number of points in file

data on line 3: x(0), y(0), pen(0), x(1), y(1), pen(1), x(2), y(2), pen(2)

data on line 4: follows like line 3... to end

note: there may not be three x,y,pen combos per line. could be 1, 2, or 3.

so far I have the following:

import sys
import os
import numpy as np

filepath = 'The_Ark.top'
with open(filepath) as file:
    data = file.readlines()

lineCount = len(data)

# parse first line
firstLine = data[0]
words = firstLine.split()
objectName = words[0]
mirrorCard = int(words[1])
if mirrorCard == 0:
    mirrorFlag = "True"
else:
    mirrorFlag = "False"
    
xMin = float(words[2])
xMax = float(words[3])
yMin = float(words[4])
yMax = float(words[5])

xCenter = (xMax - xMin)/2 + xMin
yCenter = (yMax - yMin)/2 + yMin

# parse second line
secondLine = data[1]
words = secondLine.split()
numPoints = int(words[0])

# parse remaining lines
.
.
.
# having trouble here...
.
.
.

    
print ("\nRead %d lines\n" % lineCount)

print ("File Name: " + objectName + " and mirror set to: " + mirrorFlag)
print ("xMin: %7.3f  xMax: %7.3f" % (xMin, xMax))
print ("yMin: %7.3f  yMax: %7.3f" % (yMin, yMax))
print ("x Center: %7.3f  y Center: %7.3f" % (xCenter, yCenter))
def regular_line_parse(data, line_number):
    line_index = line_number - 1
    scope_data = data[line_index]
    line_parts = scope_data.split()
    cluster_size = len(line_parts) / 3

    X, Y, PEN = [], [], []
    for i in range(cluster_size):
        X.append(float(line_parts[3 * i]))
        Y.append(float(line_parts[3 * i  + 1]))
        PEN.append(float(line_parts[3 * i + 2]))
   
    return X, Y, PEN

This function should help you in the trouble area that you marked. It parses the specific line number of data(line number > 2 in your case) and returns each type of value as a list so that you could save them as you'd wish.

You can store all of the points from line 3 and beyond in a list of lists.

You just need to replace:

# parse remaining lines
.
.
.
# having trouble here...
.
.
.

with:

line = list()
points = list()

for i in range(2,len(data)):
    line.extend(data[i].split())

points = [line[x:x+3] for x in range(0, len(line),3)]

or if you want to store each of them as separate lists, you can do the following:

x = list()
y = list()
pen = list()

for i in range(2,len(data)):
    line = data[i].split()
    for j in range(len(line)):
        if j%3 == 0:
            x.append(line[j])
        elif j%3 == 1:
            y.append(line[j])
        else:
            pen.append(line[j])

You can make plots easily this way.

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