0

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'] ])
5
  • 3
    str(my_list). Commented Aug 11, 2022 at 3:07
  • 2
    str(my_list)? Commented Aug 11, 2022 at 3:07
  • 2
    str(my_list)! Commented Aug 11, 2022 at 3:07
  • 1
    f'"{str(my_list)}"' Commented Aug 11, 2022 at 3:13
  • What, exactly, are you trying to do? Commented Aug 11, 2022 at 3:44

2 Answers 2

2

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'] ])
Sign up to request clarification or add additional context in comments.

Comments

1

you can just use the str function

ans = str(my_list)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.