亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當格式特定時,如何使用 Python 逐行讀取文本文件

當格式特定時,如何使用 Python 逐行讀取文本文件

慕姐8265434 2023-05-09 10:20:09
當格式特定時,如何使用 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), pen(0), x(1), y(1), pen(1), x(2), y(2), pen(2)第 4 行的數據:跟第 3 行一樣……結束注意:每行可能沒有三個 x,y,pen 組合。可以是 1、2 或 3。到目前為止,我有以下內容:import sysimport osimport numpy as npfilepath = 'The_Ark.top'with open(filepath) as file:    data = file.readlines()lineCount = len(data)# parse first linefirstLine = 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 + xMinyCenter = (yMax - yMin)/2 + yMin# parse second linesecondLine = 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))
查看完整描述

2 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

您可以將第 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])


您可以通過這種方式輕松制作繪圖。


查看完整回答
反對 回復 2023-05-09
?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

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)并將每種類型的值作為列表返回,以便您可以根據需要保存它們。


查看完整回答
反對 回復 2023-05-09
  • 2 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號