小编Tbe*_*s88的帖子

将文本文件转换为 numpy 数组

我正在尝试将字符串列表转换为数组。该列表实际上是我从文本文件中获取的 n 行长 4 列的数字数组。我需要将此列表转换为 n 行 x 4 列的浮动类型数组。以下是我到目前为止的代码:

#Calculate the average velocity through a tidal cycle

from pylab import *
import numpy as np

#Open profile1.ele and get the data
lookup = '##'
data = []
eleline = []

with open('profile1.ele') as f:
for line in f:
    if not line.startswith(lookup):   #disclude lines with '##'
        data.append(line.rstrip("\r\n"))  
    if 'Elements' in line:
        eleline.append(line)
s = ''.join(eleline)   #Convert list to string
numele = s.rsplit()[-2] #Grab # of elements
numele = int(numele) #convert …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

arrays ×1

numpy ×1

python ×1