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?
#4
(Apr-05-2020, 05:36 PM)buran Wrote: please, show real code, don't explain what you have or what you expect. As of now it's not clear neither what you have, or what you want.
From what i get - the way you write is not correct (i.e. you will get also square brackets in the file).
For reading csv file - look at csv module or read the line and split at ','.
Note in any case you will need to convert each element to int, because they will come as str from the file.

Thanks. I understand the importance of using code quotations and will do so from now on. Yes I did get square brackets in the file which is what I thought I had said. Anyway I found the use of pickle to do what I needed.

import pickle

add_val = 110
att_val = 7
val_val = 125

val = [add_val, att_val, val_val]

print(val)

with open('test', 'wb') as f:
    pickle.dump(val, f)

with open('test', 'rb') as f:
    z = pickle.load(f)

print(z)

print('add_val = ', z[0])
print('att_val = ', z[1])
print('val_val = ', z[2])
And here is the output

>>> %Run pickletest.py
[110, 7, 125]
[110, 7, 125]
add_val =  110
att_val =  7
val_val =  125
Reply


Messages In This Thread
RE: How do I convert this string back to a list of integers? - by donmerch - Apr-05-2020, 05:48 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,316 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Convert dataframe from str back to datafarme Creepy 1 672 Jul-07-2023, 02:13 PM
Last Post: snippsat
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,375 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,600 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,542 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 2,014 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,129 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,533 Dec-30-2022, 05:38 PM
Last Post: dee
  convert a list to links Pir8Radio 3 1,161 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,591 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