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>You should use the python CSV module. Look here for more infromation, <a href="http://stackoverflow.com/questions/6916542/writing-list-of-strings-to-excel-csv-file-in-python">Writing List of Strings to Excel CSV File in Python</a>. Here is some example for your problem. </p>

<pre><code>import csv
results = []

# your code... also add this to your for loop.
    results.append([title, link])

csv_file = open("csv_file.csv",'wb')
wr = csv.writer(csv_file)
for row in results:
    wr.writerow(row)
</code></pre>

python python-3.x csv pandas