新手 python csv writer 问题:为什么每个字符分开?
请原谅我问一个简单的问题:我尝试使用 csv 模块编写简单的 csv 文件。但是,结果是这样的:
Spam |Baked Beans|
/ s e a r c h | | , | | A d v a n c e d | | S e a r c h
/ a b o u t / | | , | | A b o u t
/ n e w s / | | , | | N e w s
/ d o c / | | , | | D o c u m e n t a t i o n
/ d o w n l o a d / | | , | | D o w n l o a d
/ c o m m u n i t y / | | , | | C o m m u n i t y
/ p s f / | | , | | F o u n d a t i o n
/ d e v / | | , | | C o r e | | D e v e l o p m e n t
/ a b o u t / h e l p / | | , | | H e l p
我使用的代码是:
spamWriter = csv.writer(open('links.csv', 'w'), delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamWriter.writerow(['Spam'] + ['Baked Beans'])
spamWriter.writerow(self.linkvalue + ' , ' + data)
linkvalue 和 data 是保存一些数据的两个变量。
谢谢你的建议!!!!
please excuse me for simple question: i tried to write simple csv file using csv module. however, the result is this:
Spam |Baked Beans|
/ s e a r c h | | , | | A d v a n c e d | | S e a r c h
/ a b o u t / | | , | | A b o u t
/ n e w s / | | , | | N e w s
/ d o c / | | , | | D o c u m e n t a t i o n
/ d o w n l o a d / | | , | | D o w n l o a d
/ c o m m u n i t y / | | , | | C o m m u n i t y
/ p s f / | | , | | F o u n d a t i o n
/ d e v / | | , | | C o r e | | D e v e l o p m e n t
/ a b o u t / h e l p / | | , | | H e l p
the code i use is:
spamWriter = csv.writer(open('links.csv', 'w'), delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamWriter.writerow(['Spam'] + ['Baked Beans'])
spamWriter.writerow(self.linkvalue + ' , ' + data)
linkvalue and data are two variables that hold some data.
thank you for your advice!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
writerow 的参数是一个序列...而您要传递的字符串是单个字符的序列。要修复您的错误,请在第二次调用
writerow
时,传递[self.linkvalue, data]
作为参数。writerow
's argument is a sequence... and a string, which is what you're passing, is a sequence of single characters. To fix your bug, in the 2nd call towriterow
, pass, instead,[self.linkvalue, data]
as the argument.