【问题标题】:ValueError in Python 3; specifically jupyter notebookPython 3 中的值错误;特别是 jupyter 笔记本
【发布时间】:2018-10-31 16:19:51
【问题描述】:

我正在尝试读取文件

文本文件本身分为 9 列,包含大量数据(总共 454 行)

我正在尝试读取和检索某些数据列,以便绘制与温度相关的质量图(HR 图)

但是,当我尝试使用以下方式加载文本时:

file = 'nameoftext.txt' #the file itself is saved as a txt from notepad++
track1 = np.loadtext(file, skiprows=70) #im skipping 70 rows of headers to the data (and np is numpy)

我收到一条错误消息:

ValueError: could not convert string to float: 'iso'

我不知道这意味着什么或我在做什么。

我也在使用np.loadtext,因为这是我的教授向我们展示如何加载文件的唯一方法,我不知道该怎么做。

【问题讨论】:

  • Numpy 只接受数字。 CSV 中一定有混合字符串。
  • @riddler 这不完全正确,您可以将具有正确编码的字符串放入 numpy 数组中
  • 如果您知道 9 列的数据类型(您应该知道),您可以像 dtype='str,int, int, str, int, int, str, float, int', 一样手动指定它们。否则,您可以使用np.genfromtxt 并指定dtype=None。看看here
  • 无需为此责怪 Jupyter。只是说

标签: python python-3.x jupyter-notebook


【解决方案1】:

在 python 中加载 .txt 文件的另一个选项是 numpy 中的 genfromtxt() 函数。在此函数中,可以指定每列中值的对象类型,也可以让函数自行猜测类型。

查看下面的链接以了解类似问题。 Loading text file containing both float and string using numpy.loadtxt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    相关资源
    最近更新 更多