要将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'

最佳答案

您正在尝试将字符串保存为支持数字的格式。不要将圆钉塞到方孔中,而要取一个圆孔,例如.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中工作,但解析纯字符串以形成合适的格式应该不会太麻烦。

07-24 09:52
查看更多