python - 将 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'
解决方案
您正在尝试将字符串保存为支持数字的格式。不要把圆钉塞进方孔,而是要一个圆孔,例如.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 中工作,但是将纯字符串解析为合适的格式应该不会太麻烦。
推荐阅读
- php - 如何统计在线用户?
- c# - C# 在 SqlDataReader 中禁用自动格式化
- java - 如何使用 Tika API 查找 .bat 文件的 mime 类型?
- reactjs - 设置 React Router 的优先级
- idris - 我可以避免在 Idris 的全部功能中明确排除无效案例吗?
- android - 使用 mockk 的测试用例无法正常工作
- python - 将聊天符号转换为表情符号或文本
- node.js - 使隐藏的 vc 变为可见的 discord.js
- java - 接口引用是否需要将强制转换分配给类对象引用?
- asp.net - 无法仅从我的 asp .net 日历控件中获取日期