numpy的loadtxt单线/行作为列表 [英] numpy loadtxt single line/row as list

查看:142
本文介绍了numpy的loadtxt单线/行作为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据文件只有一个像行:

I have a data file with only one line like:

 1.2  2.1  3.2

我用numpy的1.3.0版本loadtxt加载它

I used numpy version 1.3.0 loadtxt to load it

 a,b,c = loadtxt("data.dat", usecols(0,1,2), unpack=True)

输出是浮动的,而不是像数组

The output was a float instead of array like

 a = 1.2

我希望这将是:

 a = array([1.2])

如果我读多行的文件,它的工作。

If i read a file with multiple lines, it's working.

推荐答案

不使用重塑是,明确强制转换列表中的简单方法。

The simple way without using reshape is, to explicitly typecast the list

 a,b,c = loadtxt("data.dat", usecols(0,1,2), unpack=True)
 a,b,c = (a,b,c) if usi.shape else ([a], [b], [c])

这工作比重塑快!

这篇关于numpy的loadtxt单线/行作为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆