Python Can';无法保存到csv文件
我试图将数据保存到csv文件,但出现以下错误:Python Can';无法保存到csv文件,python,csv,Python,Csv,我试图将数据保存到csv文件,但出现以下错误: Traceback (most recent call last): File "H:\Yr11 Computer Science\New Lessons\Lesson 26-27\song.py", line 13, in <module> csv_file = ([song, + ',' + artist + genre]) TypeError: bad operand type for unary +: 'str'#
Traceback (most recent call last):
File "H:\Yr11 Computer Science\New Lessons\Lesson 26-27\song.py", line 13, in <module>
csv_file = ([song, + ',' + artist + genre])
TypeError: bad operand type for unary +: 'str'#
非常感谢您提供的任何帮助。这只是有关错误消息的信息。
TypeError:unary+的操作数类型错误:'str'
。这是从电话线传来的
csv_file = ([song, + ',' + artist + genre])
这意味着python无法理解+','
。如果删除第一个+
,则会删除该特定错误消息。不过,可能还有更多
python会抱怨,因为您试图使用列表
作为文件名。也许你想要
csv_file = song + ',' + artist + genre
似乎在歌曲之后不需要逗号:
csv_file = ([song, + ',' + artist + genre])
相反,请尝试:
csv_file = ([song + ',' + artist + genre])
编辑:如@hiro PROTATIONER所建议的那样-您也可以简化整行内容,并在不使用括号的情况下编写。基于其他人的建议,并基于我认为您需要的内容,我认为您应该将以下代码用于附加:
with open("songs.csv", "a") as fa:
line = song + "," + artist + "," + genre + "\n"
fa.write(line)
这将确保文件对象在追加操作后关闭,并且新的行将追加到前一行的下方,从而使生成的CSV结构化。Hi!只需删除以下行中
song
后的逗号:csv_file=([song,+','+artist+genre])
。
with open("songs.csv", "a") as fa:
line = song + "," + artist + "," + genre + "\n"
fa.write(line)