使用numpy解析csv会引发TypeError

时间:2018-06-22 09:14:53

标签: python csv numpy

我得到一个TypeError

Traceback (most recent call last):
  File "...Transients_tP100us.py", line 20, in <module>
    temp[key] = data[key]["Temperature"].reshape((num_temp, num_dp)).T
TypeError: 'numpy.int32' object does not support item assignment

运行此脚本:

import numpy as np

transient_files = {"50ms": "Transients_50ms.TXT",
                   "500ms": "Transients_500ms.TXT",
                   "5000ms": "Transients_5000ms.TXT"}

num_dp = 513
num_temp = 37

data = {}
time = {}
temp = {}
cap = {}

for key, value in transient_files.items():
    data[key] = np.genfromtxt(value, delimiter=";", names=[
                              "Time", "Temperature", "Capacitance"])

    time[key] = data[key]["Time"].reshape((num_temp, num_dp)).T    
    temp[key] = data[key]["Temperature"].reshape((num_temp, num_dp)).T
    cap[key] = data[key]["Capacitance"].reshape((num_temp, num_dp)).T * 1e-12

如果我仅运行Transient_50ms.TXT文件,则该文件运行无错误,仅抛出了另外两个文件。文件之间的唯一区别是,50ms文件在第三列中没有负值,其他列中没有。 因此,在数据文件中有无负值的两行如下所示:

 5.065E-01; 3.270E+02;-1.182E-01
 6.832E-03; 3.391E+02; 7.501E-01

我通过以下方式进行了支票

for i in np.arange(0, len(data[key]["Time"])):
    if type(data[key]["Time"][i]) is not np.float64:
        print(i)

for i in np.arange(0, len(data[key]["Temperature"])):
    if type(data[key]["Temperature"][i]) is not np.float64:
        print(i)

for i in np.arange(0, len(data[key]["Capacitance"])):
    if type(data[key]["Capacitance"][i]) is not np.float64:
        print(i)

查看所有条目是否真的被识别为np.float64(是这种情况)。我想解析数据文件是一个问题,尤其是负值,但是我不知道问题出在哪里以及如何解决。

1 个答案:

答案 0 :(得分:1)

@SpghttCd的评论改写为

您很有可能在进入第二个循环之前重新定义了<head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <title>Test D'n'D - Fancytree</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <link href="https://wwwendt.de/tech/fancytree/src/skin-win8/ui.fancytree.css" rel="stylesheet"> <script src="https://wwwendt.de/tech/fancytree/src/jquery-ui-dependencies/jquery.fancytree.ui-deps.js"></script> <script src="https://wwwendt.de/tech/fancytree/src/jquery.fancytree.js"></script> <script src="https://wwwendt.de/tech/fancytree/src/jquery.fancytree.dnd5.js"></script> <script src="https://wwwendt.de/tech/fancytree/src/jquery.fancytree.multi.js"></script> <script src="https://wwwendt.de/tech/fancytree/src/jquery.fancytree.table.js"></script> </head> <body class="example"> <h1>Example: extended drag'n'drop sample</h1> <div class="description"> This sample shows how to <ul> <li>implement drag'n'drop with multiple selected nodes <li>allow modifier keys <kbd>Ctrl</kbd> or <kbd>Alt</kbd> to force copy instead of move operations </ul> </div> <div> <label for="skinswitcher">Skin:</label> <select id="skinswitcher"></select> </div> <!-- Add a <table> element where the tree should appear: --> <!--<p class="description"> Standard tree: </p> <div id="tree"></div>--> <p class="description"> Table tree: </p> <table id="tree"> <colgroup> <col width="*"/> <col width="200px"/> <col width="100px"/> </colgroup> <thead> <tr> <th></th> <th>Key</th> <th>Folder</th> </tr> </thead> <tbody> </tbody> </table> <p class="droppable"> Droppable. </p> </body>,这似乎可以按照注释解决问题。

自从@SpghttCd在评论中回答了该问题以来,我制作了这个答案社区Wiki。