python csv模块写文件 出现 iterable expected, not int 错误
来源:互联网 发布:蜘蛛纸牌算法 编辑:程序博客网 时间:2024/06/04 18:49
开始代码:
import csvdef storFile(data,fileName): with open(fileName,'w',newline ='') as f: mywrite = csv.writer(f) for i in data: mywrite.writerow(i)data = [1,0,1,1,0,1,0]storFile(data,'splitData\wodecesi.csv')
报错:
<ipython-input-33-5de04d85deaa> in storFile(data, fileName) 4 mywrite = csv.writer(f) 5 for i in data:----> 6 mywrite.writerow(i) 7 data = [1,0,1,1,0,1,0] 8 storFile(data,'splitData\wodecesi.csv')Error: iterable expected, not int
查阅资料发现: csv 的writerow 函数只能写入一个序列,比如 list 或 array.
修改方法:
(1) 直接读取整个列表:fsdg
import csvdef storFile(data,fileName): with open(fileName,'w',newline ='') as f: mywrite = csv.writer(f) mywrite.writerow(data)data = [1,0,1,1,0,1,0]storFile(data,'splitData\wodecesi.csv')
所得csv文件中,列表元素为 1 0 1 1 0 1 0 排列;
(2)先将每个元素转换为小列表,再存储:
import csvdef storFile(data,fileName): data = list(map(lambda x:[x],data)) with open(fileName,'w',newline ='') as f: mywrite = csv.writer(f) for i in data: mywrite.writerow(i)data = [1,0,1,1,0,1,0]storFile(data,'splitData\wodecesi.csv')(3)使用pands的to_csv 函数:
data.to_csv('splitData\wodecesi.csv',index = False)
(2)和(3)所得文件中,元素为纵向排列:
1
0
1
1
0
1
0
阅读全文
0 0
- python csv模块写文件 出现 iterable expected, not int 错误
- python TypeError: 'int' object is not iterable
- python写csv文件
- python写csv文件
- python写csv文件
- python使用csv模块读写csv文件
- 【Python】python "TypeError: 'NoneType' object is not iterable"错误分析
- weka csv文件出现错误
- Python出现TypeError: 'NoneType' object is not iterable
- python csv文件写操作
- Python CSV文件写入错误
- python中csv模块写文件时总是空一行的解决办法
- python object is not iterable
- Python错误代码分析:莫名的[TypeError: 'int' object is not iterable]
- python mysql 插入数据报错 Failed processing format-parameters; 'int' object is not iterable
- Python-使用CSV模块进行文件处理
- Python csv模块读文件错误 _csv.Error: new-line character seen in unquoted field
- python 日k写csv文件
- 查找算法系列(二):哈希查找
- Servlet中的getServletContext()
- Android Fragment真正的完全解析(下)
- Nginx location匹配优先级
- STL常用方法
- python csv模块写文件 出现 iterable expected, not int 错误
- 最近找工作的心路历程
- JavaScript趣题:波浪形排序
- 每日一条Linux命令-df
- QTcpServer+QTcpSocket使用整理
- LightOJ 1030 Discovering Gold(期望)
- Collections.sort出错 Comparison method violates its general contract!
- Linux下MySQL主从复制(二)
- c++的vector赋值方法汇总