用fromi创建numpy数组

2024-06-24 12:16:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从sqlite3中存储的数据生成numpy数组,但字符串有问题。据我所知,fromiter是完成这项任务最有效的方法。获取数据后游标.fetchall(),字符串正确。但是在进入numpy数组之后,它就丢失了。在

import sqlite3
import numpy as np

connection = sqlite3.connect('fermControlData.db')

with connection:
    cursor = connection.cursor()
    sql = "SELECT Time, bTemp, aTemp, heaterStatus, gasMonitor, Location FROM dataLog WHERE beerName= 'Red Sled' ORDER BY Time"
    cursor.execute(sql)
    data = cursor.fetchall()
    print(data[0], type(data[0][5]))
    dataLog = np.fromiter(data, dtype=('f, f, f, i, i8, S'))
    print(dataLog[0])

这是输出-

^{pr2}$

如您所见,字符串“Warm1”正在转换为b“”。在

您知道为什么会发生这种情况吗?以及如何修复它以便导入字符串吗?在

非常感谢!在


Tags: 字符串importnumpysqldatatimenp数组
1条回答
网友
1楼 · 发布于 2024-06-24 12:16:40

numpy数组中的字符串是固定长度的,在fromiter的情况下,必须事先指定该长度。E、 g.使用dtype=('f, f, f, i, i8, S5')。在

相关问题 更多 >