我正在尝试将字符串列表转换为数组。该列表实际上是我从文本文件中获取的 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)