python 数组写txt_Python阅读.txt并使用numpy放入数组

1586010002-jmsa.png

i want to create an array with numpy.

The base is a .txt file which is given in the following form:

xzHfE.png

i tried it with loadtxt:

data = np.loadtxt("myfile.txt",delimiter='\n',skiprows = 1)

The first row with "Zeit" and "Signal" should be skipped i only need the array in this form:

[[0, 1], [0.01, 2], [0.02, 3]]

The Values from "Zeit" and "Signal are seperated with a tab.

i got this error:

ValueError: could not convert string to float:

解决方案

I mistakenly tried to recreate your file as

Zeit\tSignal

0.01\t1.1

0.02\t1.105

0.03\t1.108

0.04\t1.2

0.05\t1.205

0.06\t1.209

loaded with:

data=np.loadtxt('test.txt', skiprows=1)

and got:

ValueError: could not convert string to float: b'0.01\\t1.1'

Changing the string \t to real tabs (hard to convey this with copy-n-paste):

In [474]: data=np.loadtxt('test.txt', skiprows=1)

In [475]: data

Out[475]:

array([[ 0.01 , 1.1 ],

[ 0.02 , 1.105],

[ 0.03 , 1.108],

[ 0.04 , 1.2 ],

[ 0.05 , 1.205],

[ 0.06 , 1.209]])

Make sure that your file contains just these characters (with spaces or real tabs between columns). It should plain ASCII, not RTF or other formatted test.

It may be worth trying a string dtype just to see if just a few funny chacacters are giving a problem, eg.

In [478]: data=np.loadtxt('test.txt', dtype=str)

In [479]: data

Out[479]:

array([["b'Zeit'", "b'Signal'"],

["b'0.01'", "b'1.1'"],

["b'0.02'", "b'1.105'"],

["b'0.03'", "b'1.108'"],

["b'0.04'", "b'1.2'"],

["b'0.05'", "b'1.205'"],

["b'0.06'", "b'1.209'"]],

dtype='

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值