【发布时间】:2018-11-30 16:38:44
【问题描述】:
要将 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'
【问题讨论】:
-
print(''.join(a)) -
CSV 代表逗号分隔的 VALUE。你没有分离价值观。不要将圆钉塞入方孔中。只需使用
fwrite()写入任何支持字符串的格式(例如 txt、dat) -
@Adriaan 我的 matlab 知识几乎已经生锈了,但是 python 方面的答案和我的评论正在修复一些你似乎可以在上游避免的东西。如果你发布一个答案来阻止它,我更愿意投票反对修复下游。