Issue with using the zip function and writerows to create a CSV file

Question:

I am relatively new to python (3 weeks in) and am trying to create some code of scrape some data from a webpage. I have been able to use xpath to create some lists of data. So here I have a list of offices and a list of contacts.

In order to combine them, I used the zip function as you see below. If I print the list(test) I can see that I get an expected result of a list with a bunch of tuples such as

[('n101 Venture', 'nJeremy Baron'), ('n1888 Management', 'nTrent May'), ('n1919 Investment Counsel', "nHarry O'Mealia"), ('n2M Companies', 'nAmeeth Sankaran')]

Felt I was on the correct path based on this question: Write a csv file in Python : error with function writerows

However, the CSV File comes up empty whenever I open it. Here is my code:

# combine lists from scraper
test = zip(office, contact_name)

#make CSV 
with open('some3.csv', 'wt') as csvfile:
    csv_out = csv.writer(csvfile)
    csv_out.writerows(test)

So not sure why I am not generating a CSV file with each tuple as a row and each element separated by a comma in the file.

Asked By: user3335830

||

Answers:

Solved by doing this:

test = zip(office, contact_name)


#make CSV 
with open('some6.csv', 'w+', newline="") as csvfile:
    csv_out = csv.writer(csvfile, delimiter=",")
    for row in test:
        csv_out.writerow(row)
Answered By: user3335830
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.