_csv.Error:sequence expected [Python] [英] _csv.Error: sequence expected [Python]

查看:3971
本文介绍了_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. 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.

这篇关于_csv.Error:sequence expected [Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆