使用特定格式在Octave中加载文本数据

use*_*706 5 io matlab text-processing octave

我有一个我想要存储的数据集,并且能够在Octave中加载

18.0   8   307.0      130.0      3504.      12.0   70  1    "chevrolet chevelle malibu"
15.0   8   350.0      165.0      3693.      11.5   70  1    "buick skylark 320"
18.0   8   318.0      150.0      3436.      11.0   70  1    "plymouth satellite"
16.0   8   304.0      150.0      3433.      12.0   70  1    "amc rebel sst"
17.0   8   302.0      140.0      3449.      10.5   70  1    "ford torino"
15.0   8   429.0      198.0      4341.      10.0   70  1    "ford galaxie 500"
14.0   8   454.0      220.0      4354.       9.0   70  1    "chevrolet impala"
14.0   8   440.0      215.0      4312.       8.5   70  1    "plymouth fury iii"
14.0   8   455.0      225.0      4425.      10.0   70  1    "pontiac catalina"
15.0   8   390.0      190.0      3850.       8.5   70  1    "amc ambassador dpl"
Run Code Online (Sandbox Code Playgroud)

当我尝试使用时它不会立即起作用:

data = load('auto.txt')
Run Code Online (Sandbox Code Playgroud)

有没有办法从具有给定格式的文本文件加载,或者我需要将其转换为例如

18.0,8,307.0,130.0,3504.0,12.0,70,1
...
Run Code Online (Sandbox Code Playgroud)

编辑: 删除最后一行并修复"半"数字,例如3504. - > 3504.0,然后使用:

data = load('-ascii','autocleaned.txt');
Run Code Online (Sandbox Code Playgroud)

将所需数据加载到Octave中的矩阵中.

Sha*_*ful 5

load通常用于加载八度音程和Matlab二进制文件,但可以用于加载文本数据(如您的文本数据)。您可以使用该"-ascii"选项加载数据,但是load即使"-ascii"启用了该选项,也必须在重新格式化文件之前将其稍作格式化。使用一致的列分隔符,即。只是一个制表符或逗号,3850.请不要使用整数,也不要使用字符串。

然后,您可以执行以下操作使其正常工作

DATA = load("-ascii", "auto.txt");
Run Code Online (Sandbox Code Playgroud)


hpa*_*ulj 5

如果从每一行中删除了最后的字符串字段,则可以使用以下内容读取该文件:

filename='stack25148040_1.txt'
fid = fopen(filename, 'r');
[x, count] = fscanf(fid, '%f', [10, Inf])
endif
fclose(fid);
Run Code Online (Sandbox Code Playgroud)

或者,整个文件可以作为一列读入并重新整形.

我还没弄明白如何读取数字字段和字符串字段.为此,我不得不使用更通用的文件阅读工具来回退Python.

这是一个Python脚本,它读取文件,创建numpy结构化数组,将其写入.mat文件,然后Octave可以读取:

import csv
import numpy as np

data=[]
with open('stack25148040.txt','rb') as f:
    r = csv.reader(f, delimiter=' ')
    # csv handles quoted strings with white space
    for l in r:
        # remove empty strings from the split on ' '
        data.append([x for x in l if x])
print data[0]
for dd in data:
    # convert 8 of the strings (per line) to float
    dd[:]=[float(d) for d in dd[:8]]+dd[-1:]

data=data[:-1]  # remove empty last line
print data[0]
print
# make a structured array, with numbers and a string
dt=np.dtype("f8,i4,f8,f8,f8,f8,i4,i4,|S25")
A=np.array([tuple(d) for d in data],dtype=dt)
print A
from scipy.io import savemat
savemat('stack25148040.mat',{'A':A})
Run Code Online (Sandbox Code Playgroud)

Octave这可以阅读

load stack25148040.mat
A
# A = 1x10 struct array containing the fields:
#    f0 f1 ... f8

A.f8  # string field
A(1)  # 1st row
#  scalar structure containing the fields:
#   f0 =  18
#   f1 = 8
...
#   f8 = chevrolet chevelle malibu
Run Code Online (Sandbox Code Playgroud)

较新的Octave(3.8)具有importdata功能.它处理原始数据文件,没有任何额外的参数.它返回一个包含2个字段的结构

x.data是一个(10,11)矩阵. x.data(:,1:8)是欲望的数值数据. x.data(:,9:11)NA随机数字的混合.将NA在该行的最后这几个字特别引人注目英寸 x.textdata是一个(24,1)带有这些词的单元格.引用的字符串s可以从这些单词中重新组合,使用NA和引号来确定有多少单词属于哪一行.

读取它使用的数字数据dlmread.由于其余部分importdata是写入的Octave,因此可以将其用作正确处理字符串数据的自定义函数的起点.

dlmread ('stack25148040.txt')(:,1:8)
importread ('stack25148040.txt').data(:,1:8)
textread ('stack25148040.txt','')(:,1:8)
Run Code Online (Sandbox Code Playgroud)