python代码提示expected,_csv.Error:sequence expected [Python]

这篇博客探讨了在使用Python的csv模块时遇到的'sequence expected'错误。作者试图创建一个CSV文件,每行包含从1到1000的ID。问题在于`numbersOut()`函数没有返回值,导致`out.writerow(numbersOut())`调用时出现错误。解决方案是修改`numbersOut()`函数,使其接收一个参数并直接写入行。同时建议将单个字符串写入改为列表,以便csv模块正确解析为一行。最后,建议移除手动添加的换行符,因为`csv.writer`会在写入行时自动处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here's the entirety of my code:

import csv

def numbersOut():

for i in range (1, 1001):

out.writerow("%s" % (i+1, ))

out.writerow("\n")

csvname = raw_input("Enter desired filename: ")

headers = ["ID", "Student", "Grade"]

out = csv.writer(open(csvname + ".csv","w"), delimiter=',', lineterminator='\n', quoting=csv.QUOTE_ALL)

out.writerow(headers)

out.writerow(numbersOut())

And the section that's throwing up the "sequence expected" error is line 14, namely this one: out.writerow(numbersOut())

Basically, what I'm trying to do is use the function numbersOut() to put 1000 rows in a CSV file, each with it's own ID (Line 1 (Minus headers) will = 1, Line 2 will = 2, etc). I managed to get them all on one row by using range, but that's not what I'm needed.

However, I keep getting an error when trying to use my function. I'm not sure if it's a problem with how I'm calling it or whether it's a problem within the function itself.

Any ideas? If anything needs clarifying, please ask; I have a habit of not making much sense.

Thanks in advance!

EDIT: Traceback, as requested:

Traceback (most recent call last):

File "writer.py", line 14, in

out.writerow(numbersOut())

_csv.Error: sequence expected

解决方案

In Python, if a function falls off the end without returning, it is assumed to return None. Your numbersOut() function doesn't return anything. So, when you write

out.writerow(numbersOut())

you end up calling out.writerow(None). This will give you the error you're seeing, as None isn't a sequence and the csv module doesn't know how to write out None as a row of CSV data.

I think you would be better off declaring numbersOut to take out as an argument:

def numbersOut(out):

for i in range (1, 1001):

out.writerow("%s" % (i+1, ))

out.writerow("\n")

and then call it using

numbersOut(out)

There are another couple of changes I'd recommend making.

If you only want to write a single value to a row, replace the line

out.writerow("%s" % (i+1, ))

with

out.writerow(["%s" % (i+1, )])

(note the inserted [ and ]).

writerow takes a sequence of values and writes each value out into a separate comma-separated cell. If you pass a single string, that gets interpreted as a sequence of characters, and each character ends up in its own cell.

Secondly, I'd recommend removing the line out.writerow("\n"). When you write a row using writerow, the csv module will output the end-of-line for you. There's no need for you to end the lines manually. In fact, the line out.writerow("\n") ends up writing a cell containing a newline between each row that contains a number.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值