input.txt中 -如何在python中逐行讀寫.txt文件?
I am Hungry
call the shopping mall
connected drive
我想逐行讀取input.txt的線和發送作爲對服務器的請求,後來分別保存響應。如何逐行讀寫數據?
我的代碼如下僅適用於input.txt中的一個輸入(例如:我餓了)。你能幫我怎麼做多輸入?
請求:
fileInput = os.path.join(scriptPath, "input.txt")
if not os.path.exists(fileInput):
print "error message"
Error_Status = 1
sys.exit(Error_Status)
else:
content = open(fileInput, "r").read()
if len(content):
TEXT_TO_READ["tts_input"] = content
TEXT_TO_READ = json.dumps(TEXT_TO_READ)
else:
print "error message 2"
request = Request()
響應:
res = h.getresponse()
data = """MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=--Nuance_NMSP_vutc5w1XobDdefsYG3wq
""" + res.read()
msg = email.message_from_string(data)
for index, part in enumerate(msg.walk(), start=1):
content_type = part.get_content_type()
payload = part.get_payload()
if content_type == "audio/x-wav" and len(payload):
with open('Sound_File.pcm'.format(index), 'wb') as f_pcm:
f_pcm.write(payload)
elif content_type == "application/json":
with open('TTS_Response.txt'.format(index), 'w') as f_json:
f_json.write(payload)
你能告訴我怎麼做? – sam