使用loadtxt在单个str列中读取 [英] Reading in single str column with loadtxt

查看:145
本文介绍了使用loadtxt在单个str列中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为mda_bk-adds-gro.inp的文件:

# -*- mode:python -*-
0.5, 0.5, 0.5, walp_fixed.gro
0.5, 0.5, 0.4, walp.gro

我想我将分别阅读数字和单词.我成功解析了数字:

I think I'll read the numbers and the the word separately. I've succeded in parsing the numbers:

loadtxt('mda_bk-adds-gro.inp', comments='#', delimiter=',', usecols=(0,1,2))

但不能只用文字来阅读:

But can't read in just words:

loadtxt('mda_bk-adds-gro.inp', comments='#', delimiter=',', dtype=[('fileName', '|S100')], usecols=(3))

它给出了一个错误:

 TypeError: 'int' object is not iterable

所以我的问题是-如果列为str,如何用loadtxt读取第四列?

So my question is - how do I read the forth column with loadtxt provided the column is str?

推荐答案

之所以得到TypeError,是因为(3)不是元组,而只是带括号的int型表达式.尝试使用usecols=(3,).

You get the TypeError because (3) is not a tuple, but just a parenthesized int-typed expression. Try usecols=(3,) instead.

请参见此问题上的评论,以获取解释.

See the comments at this issue for an explanation why this is so.

这篇关于使用loadtxt在单个str列中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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