creating a csv file from a function result, python
<p>I am using this pdf to csv function from {<a href="http://stackoverflow.com/questions/25665/python-module-for-converting-pdf-to-text">Python module for converting PDF to text</a>} and I was wondering how can I now export the result to a csv file on my drive? I tried adding in the function </p>

<pre><code>with open('C:\location', 'wb') as f:
    writer = csv.writer(f)
    for row in data:
        writer.writerow(row)
</code></pre>

<p>but the resulting csv file has one character per row and not the rows I have when printing data in python.</p>

<p>If you are printing a single character per row, then what you have is a string.  Your loop</p>

<pre><code>for row in data:
</code></pre>

<p>translates to</p>

<pre><code>for character in string:
</code></pre>

<p>so you need to break your string up into the chunks you want written on a single row.  You might be able to use something like <code>data.split()</code> but it's hard to say without seeing more of your code and data.</p>

<p>In response to your comment:
yes, you can just dump the data to a CSV... If it adheres to the rules of CSV.  If your data is separated by commas, with each row terminated by a newline, then you can just write your data to a file.</p>

<pre><code>with open ("file.csv",'w') as f:
  f.write(data)
</code></pre>

<p>This will ONLY work if your data adheres to the rules of csv.</p>

python csv pdf