0

My project requires a JSON file to remember a string of words. The code converts a list:

lst = ['Hello', ' there', ' world!']

Into a string on the JSON file. It does this by using the code:

lst = "".join(lst)
jsonFile = open("example.json", "w")
jsonFile.write(json.dumps(lst))
jsonFile.close()

New example.json:

"Hello there world!"

How do I then convert example.json back into lst as a usable list?

NewToPython
  • 97
  • 3
  • 9

2 Answers2

1

Assuming you would like to convert a string of words into a list:

my_string = "Hello there world!"

print(my_string.split()) # gives you ['Hello', 'there', 'world!']

Is this the output you were looking for? I suspect the additional quotation marks is because you might have typecasted the string again before splitting it into a list

Based on the edited question:

lst = ['"Hello there world!"'] 
output_string = ''.join(map(str, lst)).replace('"', "")
print(output_string.split())
  • `my_string.split()` is already a list. Also, by default `split` will split by space. – Chiheb Nexus Jun 26 '21 at 16:15
  • Well I need to then convert the string in the JSON file back into a list. How would I do that? – NewToPython Jun 26 '21 at 16:25
  • 1
    I see you have edited the question further. To answer it, would you prefer all the json contents into a single list? If so, you can read each line and append to a list. Else, you can have a list for every line in the json. Can you give an example of how the json file looks like? – RyomenSukuna Jun 26 '21 at 16:27
  • A single list. I would want the list “lst” to essentially paste example.json into itself as a list of words. Does that make sense? – NewToPython Jun 26 '21 at 16:33
  • When you say 'JSON file' that means the file contents consist of key, value pairs. For eg, {name: "John Doe", age:"50"}. Going by your ask, would you prefer your list to be `lst = ["John Doe", "50"]` ? – RyomenSukuna Jun 26 '21 at 16:39
  • Not exactly. I have my code to where it dumps a list as a string into ```example.json```. The original list looks like the contents of ```lst``` in the question above. I want to convert the string in ```example.json``` into a list exactly like the original list above. My question is how I would code my program to do that. Does that help? – NewToPython Jun 26 '21 at 16:56
  • `lst = ['Hello', 'there', 'world!'] print(' '.join(map(str, lst)))` Try this. Does this answer your issue? – RyomenSukuna Jun 26 '21 at 17:07
  • I’m asking how I would extract the string from ```example.json``` and turn it back into a list that looks like ```[‘Hello’, ‘there’, ‘world!’]```. – NewToPython Jun 26 '21 at 17:10
  • https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list Can you check this and apply the logic mentioned in the answer? This should help with your issue. Keeping in mind that your `example.json` file consists of strings rather than the typical dictionary as you mentioned – RyomenSukuna Jun 26 '21 at 17:18
  • Thank you for that! That extracts the string from ```example.json```. The problem is that it converts it to ```[‘“Hello there world!’]```. How do I then convert that into a list of words and not contain the quotation marks? – NewToPython Jun 26 '21 at 17:32
  • `lst = ['"Hello there world!"'] output_string = ''.join(map(str, lst)).replace('"', "") print(output_string.split()) ` This is a quick fix. It returns ['Hello', 'there', 'world!']. IMO, the solution isn't great, so I'll make it more nicer, but this should take care of the extra quotations – RyomenSukuna Jun 26 '21 at 18:09
  • Thanks so much! I’ve been stumped on the solution for a while. – NewToPython Jun 26 '21 at 21:35
0

You can dump lst directly to json, no joining needed:

import json

lst = ['Hello', ' there', ' world!']

with open('example.json', 'w') as f:
    json.dump(lst, f)

You can then load the list from the file:

with open('example.json') as f:
  lst = json.load(f)

In case you wish or need to stick to your original code to save the file, you can load it like this:

with open('example.json') as f:
  data = json.load(f)
  lst = data.split()
  #lst = [lst[0]] + [' ' + i for i in lst[1:]] #if you want to reinsert the spaces
RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26