arrays - How to access the object in python? -
text_to_read = """{ "tts_type": "text", "tts_input": "i hungry" }"""
my old code :
scriptpath = os.path.abspath(__file__) scriptpath = os.path.dirname(scriptpath) line = os.path.join(scriptpath, "input.txt") text_to_read["tts_input"] = line
i trying access tts_input of text_to_read. think, making mistake. can 1 me , how access ?
i not able read 4 line of code , getting error : text_to_read[tts_input] = line attributeerror: 'str' object has no attribute 'tts_input'. can me ?
you need parse text_to_read
object. can use python library json achieve that.
import json text_to_read = """{"tts_type": "text","tts_input": "i hungry"}""" text_to_read = json.loads(text_to_read) # rest of code text_to_read["tts_input"] = line
of course, if text_to_read
string defined in code (not pulled database or whatever, use modification @deleisha suggested). if need have string, json.loads() parse object.
update
since have bug in updated question, i'll update answer. in new code, have
text_to_read1 = json.loads(text_to_read)
and later
text_to_read["tts_input"] = line
what did saved object text_to_read1 , again tried pull "tts_input" property string. please, modify text_to_read1["tts_input"] = line
update 2
ok, so, if understand code correctly, request.add_json_parameter() should accept string data
argument. in assignment text_to_read["tts_input"] = line
wanted update original string. after should convert string again, json.dumps()
therefore, after line
text_to_read["tts_input"] = line
add
text_to_read = json.dumps(text_to_read)
update 3 (as answer comment)
you have change
line = os.path.join(scriptpath, "input.txt") text_to_read["tts_input"] = line
to this
line = os.path.join(scriptpath, "input.txt") text_to_read["tts_input"] = open(line, "r").read()
in order read contents of file.
Comments
Post a Comment