[英]Python reading a text file into a 2D array and accessing the data
I am trying to read data from a text file into a 2D array and then access each element of the data. 我正在尝试将数据从文本文件读取到2D数组中,然后访问数据的每个元素。 I have tried a number of different approaches but I am unable to access each element of the data,
我尝试了多种方法,但无法访问数据的每个元素,
Here is an extract of the data, 这是数据的摘录,
GRID 16 7.5 5.961539 0.
GRID 17 7.5 11.92308 0.
GRID 18 7.5 17.88461 0.
GRID 19 7.5 23.84615 0.
GRID 20 7.5 29.80769 0.
GRID 21 7.5 35.76923 0.
GRID 22 7.5 41.73077 0.
GRID 23 7.5 47.69231 0.
GRID 24 7.5 53.65384 0.
Using the example here, Import nastran nodes deck in Python using numpy 使用此处的示例, 使用numpy在Python中导入nastran节点卡片组
It imports OK but it as a 1D array and I 'ary[1,1]' for example, I get the following response, 它导入了OK,但是将其作为一维数组,例如,我是“ ary [1,1]”,我得到以下响应,
x[1,1]
Traceback (most recent call last):
File "<ipython-input-85-3e593ebbc211>", line 1, in <module>
x[1,1]
IndexError: too many indices for array
What I am hoping for is, 我希望的是
17
I have also tried the following code and again this reads into a 1D array, 我还尝试了以下代码,再次将其读入一维数组,
ary = []
with open(os.path.join(dir, fn)) as fi:
for line in fi:
if line.startswith('GRID'):
ary.append([line[i:i+8] for i in range(0, len(line), 8)])
and I get the following error, 我收到以下错误,
ary[1,2]
Traceback (most recent call last):
File "<ipython-input-83-9ac21a0619e9>", line 1, in <module>
ary[1,2]
TypeError: list indices must be integers or slices, not tuple
I am new to Python but I do have experience with VBA where I have used arrays a lot, but I am struggling to understand how to load an array and how to access the specific data. 我是Python的新手,但是我确实有使用VBA的经验,在VBA中我经常使用数组,但是我一直在努力了解如何加载数组以及如何访问特定数据。
You can use genfromtxt
function. 您可以使用
genfromtxt
函数。
import numpy as np
ary = np.genfromtxt(file_name, dtype=None)
This will automatically load your file and detect fields type. 这将自动加载文件并检测字段类型。 Now you can access
ary
by row or by column, for example 现在,您可以按行或按列访问
ary
,例如
In: ary['f1']
Out: array([16, 17, 18, 19, 20, 21, 22, 23, 24])
In: ary[2]
Out: (b'GRID', 18, 7.5, 17.88461, 0.)
or by single element: 或按单个元素:
In: ary[3]['f1']
Out: 19
In: ary['f1'][3]
Out: 19
You are importing it from a text file? 您是从文本文件导入它吗? Can you save the text file as a csv?
您可以将文本文件另存为csv吗? If so, you can easily load the data using pandas.
如果是这样,您可以轻松地使用熊猫加载数据。
import pandas as pd
data = pd.read_csv(path_to_file)
Also, it might be that you just need to reshape your numpy array using something like: 另外,可能您只需要使用类似以下方法来重塑numpy数组即可:
x = x.reshape(-1, 4)
EDIT: Since your format is based on fixed width, you would want to use the fixed width in pandas instead of read_csv. 编辑:由于您的格式基于固定宽度,因此您希望在熊猫中使用固定宽度而不是read_csv。 Example below uses width of 8.
下面的示例使用宽度8。
x = pd.read_fwf(path_to_file, widths=8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.