当格式是特定的时,如何使用Python读取linebyline中的文本文件

2024-05-19 02:55:10 发布

您现在位置:Python中文网/ 问答频道 /正文

当格式是特定的时,如何使用Python逐行读取文本文件?我的数据是“空格分隔的”,看起来是这样的,每行之间都有空格。实际上没有空行,也没有“结束”卡:

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}

第1行的数据:文件名,xMin,xMax,yMin,yMax

第2行数据:文件中的点数

第3行的数据:x(0)、y(0)、画笔(0)、x(1)、y(1)、画笔(1)、x(2)、y(2)、画笔(2)

第4行中的数据:与第3行类似…结束

注意:每行不能有三个x、y、pen组合。可以是1、2或3

到目前为止,我有以下几点:

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))

Tags: 数据importdataparsefloatwordslines空格
2条回答
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

此功能可以帮助您解决所标记的故障区域。它解析数据的特定行号(在您的情况下行号大于2),并将每种类型的值作为列表返回,以便您可以根据需要保存它们

您可以将第3行及以上的所有点存储在列表列表中

您只需更换:

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

与:

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)]

或者,如果要将每个列表存储为单独的列表,可以执行以下操作:

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])

用这种方法可以很容易地绘制绘图

相关问题 更多 >

    热门问题