Skip to content Skip to sidebar Skip to footer

Convert This List Of Lists In Csv

I am a novice in Python, and after several searches about how to convert my list of lists into a CSV file, I didn't find how to correct my issue. Here is my code : #!C:\Python27\re

Solution 1:

Simply use pandas dataframe

import pandas as pddf= pd.DataFrame(list)
df.to_csv('filename.csv')

By default missing values will be filled in with None to replace None use

df.fillna('', inplace=True)

So your final code should be like

import pandas as pd
df = pd.DataFrame(list)
df.fillna('', inplace=True)
df.to_csv('filename.csv')

Cheers!!!

Note: You should not use list as a variable name as it is a keyword in python.

Solution 2:

I do not know if this is what you want:

list = [["hello","world"], ["my","name","is","bob"] , ["good","morning"]]
withopen("d:/test.csv","w") as f:
    writer = csv.writer(f, delimiter=";")
    writer.writerows(list)

Gives as output file:

hello;world
my;name;is;bob
good;morning

Post a Comment for "Convert This List Of Lists In Csv"