Writing values to excel csv in python
<p>I have the below code where I'm trying to write values to an excel file, but my output adds one letter in every single column, instead of the whole word, like so 
<a href="http://i.stack.imgur.com/nQsvd.png" rel="nofollow"><img src="http://i.stack.imgur.com/nQsvd.png" alt="enter image description here"></a></p>

<p>I want the whole word to be in one column. I'm currently passing in an array that has the words <code>[u'Date / Time', u'City', u'State', u'Shape', u'Duration', u'Summary']</code> into my <code>writer</code>. How can I make it so that I get the whole word in one column?</p>

<pre><code>import requests
import csv
from bs4 import BeautifulSoup

r = requests.get('http://www.nuforc.org/webreports/ndxlAK.html')
soup = BeautifulSoup(r.text, 'html.parser')
csv.register_dialect('excel')

f = open('ufo.csv', 'wb')
writer = csv.writer(f)

headers = soup.find_all('th')
header_text = []
header_count = 1
for header in headers:
    if header_count == len(headers):
        print "value being written: " + str(header_text)
        writer.writerows(header_text)
    else:
        header_text.append(header.text)
        header_count += 1

f.close()
</code></pre>

<p>You're extracting the text of a single column via:</p>

<blockquote>
  <p>for header in headers</p>
</blockquote>

<p>For each single column, you're writing it out like a row of columns via:</p>

<blockquote>
  <p>writer.writerows(header_text)</p>
</blockquote>

<p>The <a href="https://docs.python.org/2/library/csv.html#csv.csvwriter.writerows" rel="nofollow">writerows</a> method expects a list of columns to write, so it iterates over it.  You've passed it a single string, so it iterates over that and writes one character per column.</p>

<p>So either:</p>

<pre><code>writer.writerows([header_text])  # turn this single column into a list
# or
writer.writerow(header_text)  # just write out as a single item
</code></pre>

<p>should work.</p>

python excel csv