首页 > 解决方案 > 将 MATLAB 字符串数据写入 csv ,然后从 python 调用 csv 数据作为字符串

问题描述

要将 MATLAB 数据转换为 csv,我的代码是:

M={'It is raining since morning'}
csvwrite('data.csv',M)

它将句子像单个字符一样保存在单个单元格中

| I | t | | i | s | | r | a | i | n | i | n | g |......

当我从 Python 调用它时,我的代码是:

import csv
with open('data.csv') as csvfile:
    readCSV = csv.reader(csvfile)
    for row in readCSV:
        a=(row)
        print(a)

这给出了结果:

['I', 't', ' ', 'i', 's', ' ', 'r', 'a', 'i', 'n', 'i', 'n', 'g', ' ', 's', 'i', 'n', 'c', 'e', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g']

我想要的只是将这句话保存为这样的字符串,"It is raining Since Morning".

此外,如果我要发送此数据进行转换,我想将此文本转换为语音

>> ['I', 't', ' ', 'i', 's', ' ', 'r', 'a', 'i', 'n', 'i', 'n', 'g', ' ', 's', 'i', 'n', 'c', 'e', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g']

它给出了错误:

AttributeError: 'list' 对象没有属性 'strip'

标签: pythonstringmatlabcsv

解决方案


您正在尝试将字符串保存为支持数字的格式。不要把圆钉塞进方孔,而是要一个圆孔,例如.txt

M='It is raining since morning'; % Why a cell? Character arrays work better
FiD = fopen('data.txt','w+'); % Create and open file for writing
fprintf(FiD,M); % write data
fclose(FiD); % Close file

我不知道如何在 Python 中工作,但是将纯字符串解析为合适的格式应该不会太麻烦。


推荐阅读