python - 不能在 csv-read 迭代器上使用 next()
问题描述
我正在创建一个小程序来对 csv 列表进行排序。不幸的是,我无法使用 next() 方法跳过一行。我不断收到错误:
文件“file.py”,第 106 行,详细信息_sorter next(line) TypeError: 'list' object is not an iterator
但是,我正在遍历一个 csv 文件,所以我不明白为什么它一直认为它是一个列表?
with open(filename+".csv","r+") as file:
creader = csv.reader(file,delimiter=";")
with open(filename+"_ex.csv","w+") as export:
cwriter = csv.writer(export,delimiter=";")
for line in creader:
line_count = creader.line_num
colA = line[0]
colB = line[1]
colC = line[2]
colD = line[3]
colE = line[4]
colF = line[5]
colG = line[6]
colH = line[7]
colI = line[8]
if colA == '':
next(line)
解决方案
我认为 next(line) 是不必要的。您已经在行中迭代“csvreader”(即迭代器):“for line in creader:”。
我猜您想继续,或者如果行为空则跳过一行。您可以只使用“继续”而不是“下一个(行)”。
推荐阅读
- css - 根据实际定位将 CSS 三角形附加到 MatTooltip?
- javascript - 仅在页面刷新后加载反应应用程序中的样式
- json - 传单 JSON WFS 图层未出现在地图上
- yugabyte-db - 如何在 yugabyte db 中创建具有密码的用户、删除角色和拥有所有权的数据库?
- c - 从不兼容的指针类型警告初始化
- batch-file - 如果该文件夹包含超过 100 个文件,如何仅移动文件夹中的第一个文件?
- javascript - 将 js 变量发布到未以任何方法运行的 PHP
- regex - 查找该行的前 4 个字符应该是“ORA-”,接下来的 5 个字符应该是 Unix 命令/脚本中的数字
- java - 为什么要再次将数组转换为列表到数组
- css - 根据值设置材质 ui 表格单元格