4

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()
1

3 Answers 3

4

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"
3

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")

2
  • I just had to copy Rocky Li's wording for kick's and giggle's, since it works for this answer to. You can use either Pandas or CSV. CSV is built-in to Python, and is easier to learn. Pandas is meant for working with datasets, and has a lot of additional functionality surrounding it. Nov 1, 2018 at 1:33
  • Hey, I tried with this code but it's throwing some error. As you described, I tried modifying a few lines but it's not giving me the desired result. I have edited the post based on your code.
    – ShubhamA
    Nov 1, 2018 at 2:11
2

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.

2
  • Agree this is good, but does not seem to have the stated constraint of line by line Nov 1, 2018 at 1:26
  • I understand that it's quite straightforward with a data frame using pandas but it's a requirement that I have to write the output line by line using file commands. Thanks for your answer though.
    – ShubhamA
    Nov 1, 2018 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.