user2966197
user2966197

Reputation: 2981

issue in stringifying a list or list of list in python and converting it back to original form

I have been trying to stringify a list and list of list and then convert it back to it's original form. Following are my 2 sample lists:

option_list = ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']
option_listoflist = [['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600], ['1302', 'Bdg 1', 1432]]

I wrote this python code, based on some SO posts, where I am trying to stringify the above 2 lists and then try to convert them back but it is throwing error:

str1 = ''.join(option_list)
print(str1+'\n')
str_2_list = ast.literal_eval(str1)
print(str_2_list)

str2 = ''.join(option_listoflist)
print(str2+'\n')
str_2_listoflist = ast.literal_eval(str2)
print(str_2_listoflist)

When I execute this I get invalid syntax error at str_2_list = ast.literal_eval(str1).

How can I stringify a list and list of list and then convert it back to it's original form?

NOTE: What I want to do is to convert ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1'] to string version like this "['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']" and then convert it back to original list. Similarly for a list of list

Upvotes: 1

Views: 326

Answers (3)

Jay Shankar Gupta
Jay Shankar Gupta

Reputation: 6088

Your ''.join(option_list) outputs '13091P09Bdg 11226Bdg 1' which can't be converted to list because the output string is not following list syntax.

Crrected Code

option_list = ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']
option_listoflist = [['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600],['1302', 'Bdg 1', 1432]]
str1 = ','.join(option_list)
print(str1+'\n')
str_2_list = str1.split(',')
print(str_2_list)

str2 = ','.join(str(item) for innerlist in option_listoflist for item in innerlist)
print(str2+'\n')
str_2_listoflist=[str2.split(',')[i:i+3] for i in range(0, len(str2.split(',')), 3)]
for i in str_2_listoflist:
  i[2]=int(i[2])
print(str_2_listoflist)

Output:

1309,1P09,Bdg 1,1226,Bdg 1

['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']

1309,Blg 1,500,1P09,Bdg 1,4501,1226,Bdg 1,600,1302,Bdg 1,1432

[['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600], ['1302', 'Bdg 1', 1432]]

Upvotes: 0

Blender
Blender

Reputation: 298206

Since you seem to be trying to replicate JavaScript's JSON.stringify, just use the json module to do exactly that:

In [1]: import json

In [2]: option_list = ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']

In [3]: option_listoflist = [['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600], ['1302', 'Bdg 1', 1432]]

In [4]: json.dumps(option_list)
Out[4]: '["1309", "1P09", "Bdg 1", "1226", "Bdg 1"]'

In [5]: json.dumps(option_listoflist)
Out[5]: '[["1309", "Blg 1", 500], ["1P09", "Bdg 1", 4501], ["1226", "Bdg 1", 600], ["1302", "Bdg 1", 1432]]'

In [6]: json.loads(json.dumps(option_list)) == option_list
Out[6]: True

In [7]: json.loads(json.dumps(option_listoflist)) == option_listoflist
Out[7]: True

Upvotes: 1

Aleksandr K.
Aleksandr K.

Reputation: 528

You are doing it wrong - conversion to string. Try str1 = str(options_list)

Upvotes: 1

Related Questions