_csv.Error:sequence expected [Python] [英] _csv.Error: sequence expected [Python]
问题描述
这里是我的代码的全部:
import csv
def numbersOut b $ b for i in range(1,1001):
out.writerow(%s%(i + 1,))
out.writerow(\\\
)
csvname = raw_input(输入所需的文件名:)
headers = [ID,Student,Grade]
out = csv.writer (csvname +.csv,w),delimiter =',',lineterminator ='\\\
',quoting = csv.QUOTE_ALL)
out.writerow(headers)
out.writerow (numbersOut())
抛出序列期望错误的部分是第14行,即这个: out.writerow(numbersOut())
基本上,我想做的是使用函数 numbersOut()
在CSV文件中放置1000行,每行都有自己的ID(行1(减号头)将= 1,行2将= 2,等等)。我设法通过使用范围使它们在一行上,但这不是我需要的。
但是,我不断收到一个错误,当尝试使用我的函数。我不知道这是一个问题,我如何调用它,或者它是一个问题在函数本身内。
任何想法?如果有什么需要澄清,请问;我有一个没有意义的习惯。
提前感谢!
编辑:
跟踪(最近一次调用):
文件writer.py,第14行,模块>
out.writerow(numbersOut())
_csv.Error:sequence expected
在Python中,如果一个函数在没有返回的情况下结束,则假定返回 None
。您的 numbersOut()
函数不会返回
任何东西。所以,当你写
out.writerow(numbersOut())
pre>
你最后调用
out.writerow(None)
。这将给你看到的错误,因为无
不是一个序列,而且csv
模块不' t知道如何写出无
作为一行CSV数据。
我认为你会更好地声明
numbersOut
以输出
作为参数:def numbersOut(out):
for i in range(1,1001):
out.writerow(%s%(i + 1,))
out.writerow(\\\
)
然后使用
numbersOut(out)
还有另外几个我想推荐的变化。
如果你只想写一个值
out.writerow(%s%(i + 1,))
与
out.writerow([%s%(i + 1,)])
插入的
[
和]
)。
writerow
获取一系列值,并将每个值写入单独的逗号分隔的单元格。如果你传递一个字符串,它会被解释为一个字符序列,每个字符都会在自己的单元格中。
其次,我建议删除一行
out.writerow(\\\
。当使用
)writerow
写一行时,csv
模块将为您输出行尾。没有必要手动结束行。事实上,out.writerow(\\\
行最终会在包含数字的每一行之间写入一个换行符。
)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 <module> out.writerow(numbersOut()) _csv.Error: sequence expected
解决方案In Python, if a function falls off the end without returning, it is assumed to return
None
. YournumbersOut()
function doesn'treturn
anything. So, when you writeout.writerow(numbersOut())
you end up calling
out.writerow(None)
. This will give you the error you're seeing, asNone
isn't a sequence and thecsv
module doesn't know how to write outNone
as a row of CSV data.I think you would be better off declaring
numbersOut
to takeout
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 usingwriterow
, thecsv
module will output the end-of-line for you. There's no need for you to end the lines manually. In fact, the lineout.writerow("\n")
ends up writing a cell containing a newline between each row that contains a number.这篇关于_csv.Error:sequence expected [Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!