john doe john doe - 1 year ago 68
Python Question

How save an string output after a for loop in python with pandas and csv modules?

I have the following for loop:

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

How can I dump the title and link into a formmated .csv file?

Answer Source

You should use the python CSV module. Look here for more information: Writing List of Strings to Excel CSV File in Python.

Here is an example for your problem:

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:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download