我想用python从txt文件中提取数据

2024-06-25 06:33:14 发布

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

我想从所附文件中提取初始残值: http://www.filedropper.com/fixvel 但初始残差必须引用“DICPCG”行,而不是其他行(如“smoothsolver”)。然后我想将这些值存储到一个矩阵中,该矩阵包含每个时间步在同一时间步(在同一行)的初始残差值。你知道吗

真的,谢谢你


Tags: 文件comhttpwww时间矩阵残差filedropper
1条回答
网友
1楼 · 发布于 2024-06-25 06:33:14

请尝试此代码:

import re
import sys

file = open("fixVel.txt")
textfile = file.readlines()
count = -1
matrix = []
for row in textfile:
    if row.find("Time") == 0:
        count = count + 1
        matrix.append([])
    if row.find("DICPCG") == 0:
        index = row.find("Initial residual")
        index1 = row[index:].find(",")
        matrix[count].append(row[index+19:index+index1])
#print matrix
for i in matrix:
    for j in i:
        sys.stdout.write(j + " ")
    print

相关问题 更多 >