Home > OS >  Convert list in python to string
Convert list in python to string

Time:08-12

I have a list like this:

my_list = ['Food']

What I need is to convert it to a string like this:

"["food"]"

Update:

I am trying to save in csv file but it gives me an empty string.

Code:

with open("food.csv", "x", encoding="utf-8") as file:
    wr = csv.writer(file)
    wr.writerow(["Food", "Id",])
    wr.writerow(str(my_list))
    wr.writerow('1')

Last update: Solved! I just needed to do the following:

with open("languages_books.csv", "x", encoding="utf-8") as file:
    wr = csv.writer(file)
    wr.writerow(["Food", "Id",)

    wr.writerows([ [[my_list]], ['Id'] ])

CodePudding user response:

you can just use the str function

ans = str(my_list)

CodePudding user response:

Last update: Solved! I just needed to do the following:

with open("languages_books.csv", "x", encoding="utf-8") as file:
    wr = csv.writer(file)
    wr.writerow(["Food", "Id",)

    wr.writerows([ [[my_list]], ['Id'] ])
  • Related