Joiny Support
Joiny Support

Reputation: 31

Convert list in python to string

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'] ])

Upvotes: 0

Views: 80

Answers (2)

Joiny Support
Joiny Support

Reputation: 31

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'] ])

Upvotes: 2

Adrian
Adrian

Reputation: 31

you can just use the str function

ans = str(my_list)

Upvotes: 1

Related Questions