Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get a A SUGGESTION, not just a word in python

i have a list of suggestion parsed from json which look likes ['just a suggestion', 'and this too suggestion', 'dont care, this suggestion']

and i write this code:

import json
import random
import dpath.util as dp

with open('.logs') as json_file:
    data = json.load(json_file)
    res = dp.values(data, "/posts/*/com")
    file = open(".text", "w")
    file.write(str(res))
    file.close()
    file = open(".text", "r")
    tt = file.read()
    text = random.choice(tt.split())
    print(text)

but, this give to me only words. something like care or another word from list. how do i get a random suggestion from a list?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

IIUC you have a string read from a file that you want to treat like a
list so you need:

import ast
(...)
l = ast.literal_eval(tt)
text = random.choice(l)
print(text)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading