How save an string output after a for loop in python with pandas and csv modules?
<p>I have the following for loop:</p>

<pre><code>    for titles in titles:
        title = titles.xpath("a/text()").extract()
        link = titles.xpath("a/@href").extract()
        print(title, link)
</code></pre>

<p>How can I dump the title and link into a formmated .csv file?</p>

<p>Something like this (<a href="https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files" rel="nofollow">documentation</a>): </p>

<pre><code>...
doc_path = "path/to/doc.csv" # must exist, first folder on the place
                             # where you are running the script!
with open(doc_path, 'a') as doc:
    for entry in titles:
        title = entry.xpath("a/text()").extract()
        link  = entry.xpath("a/@href").extract()
        print(title, link)
        doc.write(str(title)+','+str(link)+'\n')
</code></pre>

python python-3.x csv pandas