Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

newbie python csv writer question: why every character separated?

Tags:

python

csv

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!!!!

like image 462
john Avatar asked Apr 10 '26 12:04

john


1 Answers

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 to writerow, pass, instead, [self.linkvalue, data] as the argument.

like image 154
Alex Martelli Avatar answered Apr 12 '26 16:04

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!