Python Forum

Full Version: How do I convert this string back to a list of integers?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
an easy way i think is:

str = ""
for i in values:
str += str(i)
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.
(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
Using json is preferable to use over pickle any day of the week Wink
Can keep variable information if make a dictionary,then do not need to show/recreate them in print() as you do.
>>> record =  dict(add_val=110, att_val=7, val_val=125)
>>> record
{'add_val': 110, 'att_val': 7, 'val_val': 125}
>>> record['att_val']
7
import json

record =  dict(add_val=110, att_val=7, val_val=125)
with open("my_file.json", "w") as j_out:
   json.dump(record, j_out)

with open("my_file.json") as j:
   saved_date = json.load(j)
Look at saved_date and do a print().
Output:
>>> saved_date {'add_val': 110, 'att_val': 7, 'val_val': 125} >>> saved_date['att_val'] 7 >>> for key,vaule in record.items(): ... print(f'{key} = {vaule}') ... add_val = 110 att_val = 7 val_val = 125
actually, I would recommend against using pickle.
you can use csv file or json, or some other more convenient option than pickle

import csv

add_val = 110
att_val = 7
val_val = 125
 
val = [add_val, att_val, val_val]

print(val)
print(','.join(map(str, val)))
# witing to and reading from csv file - 1
with open('test.csv', 'w') as f:
    f.write(','.join(map(str, val)))

# witing to and reading from csv file - 2
with open('test.csv', 'w') as f:
    csv_wrtr = csv.writer(f)
    csv_wrtr.writerow(val)

# reading csv file - 1
with open('test.csv') as f:
    line =  f.readline().strip()
    val = [int(value) for value in line.split(',')]
 
print(val)

# reading with csv module

with open('test.csv') as f:
    csv_rdr = csv.reader(f)
    val = [int(value) for value in next(csv_rdr)]

print(val)

# reading and writing json
import json
with open('test.json', 'w') as f:
    json.dump(val, f)

with open('test.json') as f:
    val = json.load(f)

print(val)
@snippsat was faster :-)
Ok you've convinced me... I'll try json. Thanks all.