gpt4 book ai didi

python - numpy genfromtxt 列

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:49 25 4
gpt4 key购买 nike

我在使用 numpy genfromtxt 时遇到了一个看起来非常简单的问题。这是我的代码的(非常)简化版本:

import numpy as np

in_file_1 = raw_input ('enter name of template file to be scaled:\n')
spec_1 = np.genfromtxt(in_file_1, delimiter = [8,24], dtype =float)
print spec_1

我正在阅读的文件非常简单,两列没有标题等:

6392.01   0.90286163
6392.05 0.88731778
6392.09 0.87789831
6392.13 0.87716535
6392.16 0.88523003
6392.20 0.90948176
6392.24 0.93056874
6392.28 0.95782283
6392.32 0.98056805
6392.36 0.99623797
6392.39 0.99458828
6392.43 0.9848269
6392.47 0.96011146
6392.51 0.92864767

当我在 python 命令行上使用 genfromtxt 阅读上面的内容时,它给了我预期的两列数组:

>>> import numpy as np
>>> in_file_1 = raw_input ('enter name of template file to be scaled:\n')
enter name of template file to be scaledl_1714650_052_no_head.txt
>>> spec_1 = np.genfromtxt(in_file_1, delimiter = [8,24], dtype =float)
>>> spec_1
array([[ 6.39201000e+03, 9.02861630e-01],
[ 6.39205000e+03, 8.87317780e-01],
[ 6.39209000e+03, 8.77898310e-01],
[ 6.39213000e+03, 8.77165350e-01],
[ 6.39216000e+03, 8.85230030e-01],
[ 6.39220000e+03, 9.09481760e-01],
[ 6.39224000e+03, 9.30568740e-01],
[ 6.39228000e+03, 9.57822830e-01],
[ 6.39232000e+03, 9.80568050e-01],
[ 6.39236000e+03, 9.96237970e-01],
[ 6.39239000e+03, 9.94588280e-01],
[ 6.39243000e+03, 9.84826900e-01],
[ 6.39247000e+03, 9.60111460e-01],
[ 6.39251000e+03, 9.28647670e-01]])
>>>

但是当我将它作为本文顶部的脚本 (read_test.py) 运行时,它会返回一列字符串:

[scrooge:Acc_cont_removal/All_stars/Test] darryl% python read_test.py 
enter name of template file to be scaled:
l_1714650_052_no_head.txt
[[ 6.39201000e+03 9.02861630e-01]
[ 6.39205000e+03 8.87317780e-01]
[ 6.39209000e+03 8.77898310e-01]
[ 6.39213000e+03 8.77165350e-01]
[ 6.39216000e+03 8.85230030e-01]
[ 6.39220000e+03 9.09481760e-01]
[ 6.39224000e+03 9.30568740e-01]
[ 6.39228000e+03 9.57822830e-01]
[ 6.39232000e+03 9.80568050e-01]
[ 6.39236000e+03 9.96237970e-01]
[ 6.39239000e+03 9.94588280e-01]
[ 6.39243000e+03 9.84826900e-01]
[ 6.39247000e+03 9.60111460e-01]
[ 6.39251000e+03 9.28647670e-01]]
[scrooge:Acc_cont_removal/All_stars/Test] darryl%

我已经尝试了各种分隔符配置等,但我无法弄清楚发生了什么,这段代码在几天前运行良好。使用 python v2.7 在 OSX (Lion) 上运行。

非常感谢收到的任何想法。达里尔

最佳答案

您得到的正是您应该得到的答案!

>>> numpy.genfromtxt('testfile', delimiter=[8,24], dtype = float)
array([[ 6.39201000e+03, 9.02861630e-01],
[ 6.39205000e+03, 8.87317780e-01],
[ 6.39209000e+03, 8.77898310e-01],
[ 6.39213000e+03, 8.77165350e-01],
[ 6.39216000e+03, 8.85230030e-01],
[ 6.39220000e+03, 9.09481760e-01],
[ 6.39224000e+03, 9.30568740e-01],
[ 6.39228000e+03, 9.57822830e-01],
[ 6.39232000e+03, 9.80568050e-01],
[ 6.39236000e+03, 9.96237970e-01],
[ 6.39239000e+03, 9.94588280e-01],
[ 6.39243000e+03, 9.84826900e-01],
[ 6.39247000e+03, 9.60111460e-01],
[ 6.39251000e+03, 9.28647670e-01]])
>>> print _
[[ 6.39201000e+03 9.02861630e-01]
[ 6.39205000e+03 8.87317780e-01]
[ 6.39209000e+03 8.77898310e-01]
[ 6.39213000e+03 8.77165350e-01]
[ 6.39216000e+03 8.85230030e-01]
[ 6.39220000e+03 9.09481760e-01]
[ 6.39224000e+03 9.30568740e-01]
[ 6.39228000e+03 9.57822830e-01]
[ 6.39232000e+03 9.80568050e-01]
[ 6.39236000e+03 9.96237970e-01]
[ 6.39239000e+03 9.94588280e-01]
[ 6.39243000e+03 9.84826900e-01]
[ 6.39247000e+03 9.60111460e-01]
[ 6.39251000e+03 9.28647670e-01]]

这只是打印和获取结果表示之间的区别。

>>> result = numpy.genfromtxt('testfile', delimiter=[8,24], dtype = float)
>>> repr(result)
'array([[ 6.39201000e+03, 9.02861630e-01],\n [ 6.39205000e+03, 8.87317780e-01],\n [ 6.39209000e+03, 8.77898310e-01],\n [ 6.39213000e+03, 8.77165350e-01],\n [ 6.39216000e+03,
8.85230030e-01],\n [ 6.39220000e+03, 9.09481760e-01],\n [ 6.39224000e+03, 9.30568740e-01],\n [ 6.39228000e+03, 9.57822830e-01],\n [ 6.39232000e+03, 9.80568050e-01],\n
[ 6.39236000e+03, 9.96237970e-01],\n [ 6.39239000e+03, 9.94588280e-01],\n [ 6.39243000e+03, 9.84826900e-01],\n [ 6.39247000e+03, 9.60111460e-01],\n [ 6.39251000e+03, 9.2864
7670e-01]])'

>>> str(result)
'[[ 6.39201000e+03 9.02861630e-01]\n [ 6.39205000e+03 8.87317780e-01]\n [ 6.39209000e+03 8.77898310e-01]\n [ 6.39213000e+03 8.77165350e-01]\n [ 6.39216000e+03 8.85230030e-01]\n [ 6.39220000e+03
9.09481760e-01]\n [ 6.39224000e+03 9.30568740e-01]\n [ 6.39228000e+03 9.57822830e-01]\n [ 6.39232000e+03 9.80568050e-01]\n [ 6.39236000e+03 9.96237970e-01]\n [ 6.39239000e+03 9.94588280e-01]\n [
6.39243000e+03 9.84826900e-01]\n [ 6.39247000e+03 9.60111460e-01]\n [ 6.39251000e+03 9.28647670e-01]]'

请注意 str 版本没有逗号但 repr 有吗?

关于python - numpy genfromtxt 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10396773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com