Python Forum
How do I convert this string back to a list of integers?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I convert this string back to a list of integers?
#1
Let's say I have this list where val1, etc are all integers such as val1 = 1, val2 = 15, val3 = 240

values = [val1, val2, val3] so now values = [1, 15, 240]

And I store it to a file named values.txt by using write(str(values)) and then I read it like values = read('values.txt')
and then print it it looks like this [1, 15, 240] which is what I would expect.

Now here is where I'm having trouble. How do I convert this string that was stored back into an actual list of integers that I can index each number?

Do I need to store it a different way? I know I could index each element of the list and store each element as a separate file but that isn't really what I want. I'm coding for use in a MicroPython project but testing it first using Python 3.7 in Thonny.

Thanks.
Don

I think I found a solution in the MicroPython forum. Storing the data as binary rather than text. Delete this post if you like. Thanks.
Reply


Messages In This Thread
How do I convert this string back to a list of integers? - by donmerch - Apr-05-2020, 03:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,222 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Convert dataframe from str back to datafarme Creepy 1 648 Jul-07-2023, 02:13 PM
Last Post: snippsat
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,309 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,479 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,471 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 1,955 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,067 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,486 Dec-30-2022, 05:38 PM
Last Post: dee
  convert a list to links Pir8Radio 3 1,130 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,541 Oct-21-2022, 04:25 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020