简体   繁体   English

在Python中从文件加载数组数据的最简单方法是什么?

[英]what's the easiest way to load array data from a file in Python?

I know Matlab has some nice syntax where you can put into a file array definitions, like A = [[1,2,3],... , and then you can import that file and all those definitions are read automatically. 我知道Matlab有一些不错的语法,您可以在其中放入文件数组定义,例如A = [[1,2,3],... ,然后可以导入该文件,然后自动读取所有这些定义。 I would like to do something similar in Python. 我想在Python中做类似的事情。 Basically I'm looking for the easiest way to read tabular data from a file, and having the resulting object as numpy array instances. 基本上,我正在寻找从文件中读取表格数据的最简单方法,并将结果对象作为numpy数组实例。 What's the easiest way to accomplish this? 最简单的方法是什么? (or the most Pythonic way?) (或者最Python化的方式?)

Say the data in the file is as follows: 说文件中的数据如下:

Array1
1 0 0 0
2 1 0 0
3 0.3333333333325028 0 0
4 0.6666666666657888 0 0

Array2
1 1 1 1
2 3 1 1
3 2 2 2
4 3 2 2
5 1 1 3
6 1 3 4
7 1 4 2

file test1.py : 文件test1.py

#!/usr/bin/python
a=[1,2,3,4,5,6]

file test.py : 文件test.py

#!/usr/bin/python

import test1

print test1.a

Now if you run test.py: 现在,如果您运行test.py:

$ ./test.py
[1, 2, 3, 4, 5, 6]

What Jahid said below works well if you want to put your data in Python modules. 如果您想将数据放入Python模块中,Jahid所说的话效果很好。

If on the other hand you'd rather put your data in a separate file, eg a text file, and then read it in a script, you may want to use numpy.loadtxt (it's designed to automatically read matrix-like files into numpy arrays). 另一方面,如果您希望将数据放在一个单独的文件(例如文本文件)中,然后在脚本中读取它,则可能要使用numpy.loadtxt (它旨在将类似矩阵的文件自动读取到numpy中。阵列)。

http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html

What you probably want is to put your data in the yaml file format. 您可能想要的是将数据放入yaml文件格式。 It is a text data format whose structure is based on higher-level scripting languages like Python. 它是一种文本数据格式,其结构基于Python等高级脚本语言。 You can put multiple 2D arrays of arbitrary types in it. 您可以在其中放置多个任意类型的2D数组。 However, since it is just data, not code, it isn't as dangerous as putting the data directly in a Python script. 但是,由于它只是数据,而不是代码,因此没有将数据直接放入Python脚本中那样危险。 It can pretty easily make 2D arrays , or more strictly nested lists (look at example 2.5 at that link specifically), as well as the equivalent of ordinary lists, dicts, nested dicts, strings, and any combination thereof. 它可以很容易地创建2D数组 ,或更严格地嵌套列表(具体请参见该链接的示例2.5),以及普通列表,字典,嵌套字典,字符串及其任意组合的等效项。 Since you can nest one data type in another, you can have a dictionary of 2D arrays, for example, which lets you put multiple arrays in a single file. 由于您可以将一种数据类型嵌套在另一种数据类型中,因此,例如,您可以拥有一个2D数组的字典,该字典可以将多个数组放入一个文件中。

Here is your example in yaml: 这是您在yaml中的示例:

Array1:
- [1, 0, 0, 0]
- [2, 1, 0, 0]
- [3, 0.3333333333325028, 0, 0]
- [4, 0.6666666666657888, 0, 0]

Array2:
- [1, 1, 1, 1]
- [2, 3, 1, 1]
- [3, 2, 2, 2]
- [4, 3, 2, 2]
- [5, 1, 1, 3]
- [6, 1, 3, 4]
- [7, 1, 4, 2]

And here is how to read it into numpy arrays (the file is called "temp.yaml" in my example), using the PyYaml package: 以下是使用PyYaml包将其读入numpy数组(在我的示例中该文件称为“ temp.yaml”)的方法:

>>> import yaml
>>>
>>> with open('temp.yaml') as ym:
....    res = yaml.load(ym)
>>> res
{'Array1': [[1, 0, 0, 0],
  [2, 1, 0, 0],
  [3, 0.3333333333325028, 0, 0],
  [4, 0.6666666666657888, 0, 0]],
'Array2': [[1, 1, 1, 1],
  [2, 3, 1, 1],
  [3, 2, 2, 2],
  [4, 3, 2, 2],
  [5, 1, 1, 3],
  [6, 1, 3, 4],
  [7, 1, 4, 2]]}
>>> array1 = np.array(res['Array1'])
>>> array2 = np.array(res['Array2'])
>>> print(array1)
[[ 1.          0.          0.          0.        ]
 [ 2.          1.          0.          0.        ]
 [ 3.          0.33333333  0.          0.        ]
 [ 4.          0.66666667  0.          0.        ]]
>>> print(array2)
[[1 1 1 1]
 [2 3 1 1]
 [3 2 2 2]
 [4 3 2 2]
 [5 1 1 3]
 [6 1 3 4]
 [7 1 4 2]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Python读取FoxPro DBF文件最简单的方法是什么? - What's the easiest way to read a FoxPro DBF file from Python? 在 Python 中转义 HTML 的最简单方法是什么? - What's the easiest way to escape HTML in Python? 用Python制作表格的最简单方法是什么? - What's the easiest way to make a table in Python? 从另一个 Python 文件启动 Python 文件然后在它们同时运行时通过它们发送数据的最简单方法是什么? - What is the easiest way to launch a Python file from another Python file then send data across them while they run simultaneously? 在python中将数据结构持久化到文件的最简单方法? - Easiest way to persist a data structure to a file in python? 从 blob 中提取数据并将其加载到 SQL Server 中的表中的最简单方法是什么? - What is the easiest way to pull data from a blob and load it into a table in SQL Server? 从txt文件存储数据的最简单方法? - Easiest way to store data from txt file? 将 hdf5 块加载到 python 中的单个变量的最简单方法是什么? - What is the easiest way to load hdf5 chunks to a single variable in python? 什么是迭代文件行,保持计数器的最简单方法? - What's the easiest way to iterate on a file's lines, keeping a counter? 使用pandas加载经过过滤的.tda文件的最简单方法是什么? - What is the easiest way to load a filtered .tda file using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM