Home > Software engineering >  is there a way to keep a list inside of a list of dictionaries from converting to a string when i�
is there a way to keep a list inside of a list of dictionaries from converting to a string when i�

Time:10-18

I'm having a problem when I'm writing dictir=[]variable to a CSV file, so inside the dictir=[] list there are two dictionaries and each of the two dictionaries contains a list with the key ["cities"], every time I write the list dictir=[] on the csv file for some reason the list with the key ["cities"] is converted to a string on the CSV file, is the problem on how I'm writing the list? or the whole code? any help will be great

how it's written on the data.csv file

,countrey,cities,time_visited
0,france,"['paris', 'lyon']",4
1,germany,"['Munich', 'Frankfurt']",9

how i want the data.csv file to look like , without "" on the france and germany list

,countrey,cities,time_visited
0,france,['paris', 'lyon'],4
1,germany,['Munich', 'Frankfurt'],9

here is the code i used to write on the csv file

import pandas 
import csv
dictir=[

    {"countrey":"france",
    "cities":["paris","lyon"],
    "time_visited":4}
    ,
    {"countrey":"germany",
    "cities":["Munich","Frankfurt"],
    "time_visited":9}
]


df = pandas.DataFrame(dictir) 
to_csv_file=df.to_csv ("data.csv")

CodePudding user response:

You can use the quoting=csv.QUOTE_NONE option to df.to_csv() to disable quoting. But you also need to use the escapechar option to specify an escape character that will be printed before , characters in the lists.

import csv

df.to_csv("data.csv", quoting=csv.QUOTE_NONE, escapechar='\\')

The resulting CSV file will look like:

,countrey,cities,time_visited
0,france,['paris'\, 'lyon'],4
1,germany,['Munich'\, 'Frankfurt'],9

You need either quotes or escapes so that the , inside the lists will not be treated as field separators. You'll get an error if you try to print a CSV where a field contains a comma and you haven't enabled quoting or an escape character.

CodePudding user response:

when you save as csv, it converts any lists into strings.

To convert it back into lists you can do this:

from ast import literal_eval

df["cities"] = df["cities"].apply(literal_eval)

You can save it as a pickle file (instead of csv) which will store it as a list.

  • Related