How to write a csv file line by line?

Viewed 2791

I am trying to scrape data from a website and I have collected 3 different type of information from the website. I have thousands of records in the 3 list but for simplicity, I am adding a few records.

List1 = ['DealerName']
List2 = ['Person1','Person2']
List3 = ['crisp nori, hot rice, and cold fish','takeout,fancy presentation, piled']

I have to write an output csv file line by line with 3 columns(List1, List2, List3) and the list information for the 3 columns. The 'DealerName' is constant for all records. I am facing trouble because there are commas in List3 which is separating the information in individual columns(different cells). The desired output file should look like this

List item

Thanks for the comments. Based on one of the comments, I made some modifications in the code and tried using the following code but it's not giving me the desired output.

List1 = ['DealerName']
List2 = ['Person1','Person2']
List3 = ['crisp nori, hot rice, and cold fish','takeout,fancy presentation, piled']

Output_File = open("Output.csv", "w")
Output_File.write("List1,List2,List3")

import csv, itertools
rows = itertools.zip_longest([List1, List2, List3])
c = csv.writer(Output_File)
c.writerows(rows)

Output_File.close()
3 Answers

Use csv:

import csv, itertools
rows = itertools.zip_longest(List1, List2, List3)
csvwriter.writerows(rows)

The csv module will automatically wrap strings containing comma in a quote, which will read fine.

Edit:

You can loop over the rows and output them with writerow instead of writerows, and that would fulfill your requirement of doing this line by line.

Edit 2: I've fixed my answer. itertools.zip_longest([List1, List2, List3]) should be itertools.zip_longest(List1, List2, List3)

Also you're going to want a newline after your header so Output_File.write("List1,List2,List3\n") instead of Output_File.write("List1,List2,List3")

In this particular case (in other words, not in the most general sense), specifying the first element of List1 as the fillvalue argument when calling itertools.zip_longest() looks like it would make it work:

import csv, itertools

List1 = ['DealerName']
List2 = ['Person1','Person2']
List3 = ['crisp nori, hot rice, and cold fish', 'takeout,fancy presentation, piled']

with open("Output.csv", "w", newline="") as Output_File:
    Output_File.write("List1,List2,List3\n")
    writer = csv.writer(Output_File)
    rows = itertools.zip_longest(List1, List2, List3, fillvalue=List1[0])
    writer.writerows(rows)

Contents of output.csv file afterward:

List1,List2,List3
DealerName,Person1,"crisp nori, hot rice, and cold fish"
DealerName,Person2,"takeout,fancy presentation, piled"

Use pandas:

import pandas as pd
df = pd.DataFrame([List1, List2, List3]).T 
df.to_csv('your.csv', index=False)

Pandas will automatically wrap strings containing comma in a quote, which will read fine.

Related