'str' 对象没有属性 'decode' & "write() argument must be str, not bytes"
'str' object has no attribute 'decode' & "write() argument must be str, not bytes"
我想使用 Amazon Polly 制作一个简单的 Web 应用程序。
这是我的目录结构。
.
├── data.txt
├── main.py
└── templates
└── index.html
这是我的main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import render_template
from flask import request
from flask import send_file
from boto3 import Session
import os
import csv
from io import StringIO
from contextlib import closing
app = Flask(__name__)
@app.route("/", methods=['POST', 'GET'])
def index():
if request.method == "POST":
## Sessionを作成
session = Session(aws_access_key_id='???', aws_secret_access_key='???', region_name='us-east-1')
polly = session.client("polly")
## polly.mp3が存在した場合は削除
if os.path.isfile('/tmp/output.mp3'):
os.remove ('/tmp/output.mp3')
## data.txtをCSVとして読み込む
f = open('data.txt')
reader = csv.reader(f)
## CSVを1行ずつ読み込む
for line in reader:
## CSVの1つめの項目で男女を切り替え
if line[0] == "M":
voice="Matthew"
elif line[0] == "F":
voice="Joanna"
## 音声データを作成
response = polly.synthesize_speech(
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1].decode("utf-8") + '</prosody></amazon:domain></speak>',
OutputFormat = "mp3",
VoiceId = voice, Engine='neural', TextType='ssml')
## polly.mp3に追加書き込み
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
data = stream.read()
fw = open("/tmp/output.mp3", "a+")
fw.write(data)
fw.close()
f.close()
return send_file("/tmp/output.mp3",as_attachment=True)
else:
return render_template("index.html")
if __name__ == "__main__":
app.run()
这是我的index.html
<html>
<body>
<form action="/" method="POST">
<input type="submit" value="download">
</form>
</body>
</html>
当我在本地测试此代码时(Windows10、Windows Linux、Debian、Python3.7.6 的子系统),我收到此错误消息。
File "main.py", line 41, in index
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1].decode("utf-8") + '</prosody></amazon:domain></speak>',
AttributeError: 'str' object has no attribute 'decode'
所以,我搜索了 Internet 并找到了这个页面。
我删除了 .decode('utf-8') 部分:
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1] + '</prosody></amazon:domain></speak>',
然后我收到一条新的错误消息。
File "main.py", line 50, in index
fw.write(data)
TypeError: write() argument must be str, not bytes
我搜索了 "write() argument must be str, not bytes"。很多网站建议我应该把 .decode('utf-8') 部分。
你能给我一些信息或建议吗?
正如@furas 评论的那样,
open("/tmp/output.mp3", "ba+")
工作完美。谢谢@furas!
我想使用 Amazon Polly 制作一个简单的 Web 应用程序。
这是我的目录结构。
.
├── data.txt
├── main.py
└── templates
└── index.html
这是我的main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import render_template
from flask import request
from flask import send_file
from boto3 import Session
import os
import csv
from io import StringIO
from contextlib import closing
app = Flask(__name__)
@app.route("/", methods=['POST', 'GET'])
def index():
if request.method == "POST":
## Sessionを作成
session = Session(aws_access_key_id='???', aws_secret_access_key='???', region_name='us-east-1')
polly = session.client("polly")
## polly.mp3が存在した場合は削除
if os.path.isfile('/tmp/output.mp3'):
os.remove ('/tmp/output.mp3')
## data.txtをCSVとして読み込む
f = open('data.txt')
reader = csv.reader(f)
## CSVを1行ずつ読み込む
for line in reader:
## CSVの1つめの項目で男女を切り替え
if line[0] == "M":
voice="Matthew"
elif line[0] == "F":
voice="Joanna"
## 音声データを作成
response = polly.synthesize_speech(
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1].decode("utf-8") + '</prosody></amazon:domain></speak>',
OutputFormat = "mp3",
VoiceId = voice, Engine='neural', TextType='ssml')
## polly.mp3に追加書き込み
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
data = stream.read()
fw = open("/tmp/output.mp3", "a+")
fw.write(data)
fw.close()
f.close()
return send_file("/tmp/output.mp3",as_attachment=True)
else:
return render_template("index.html")
if __name__ == "__main__":
app.run()
这是我的index.html
<html>
<body>
<form action="/" method="POST">
<input type="submit" value="download">
</form>
</body>
</html>
当我在本地测试此代码时(Windows10、Windows Linux、Debian、Python3.7.6 的子系统),我收到此错误消息。
File "main.py", line 41, in index
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1].decode("utf-8") + '</prosody></amazon:domain></speak>',
AttributeError: 'str' object has no attribute 'decode'
所以,我搜索了 Internet 并找到了这个页面。
我删除了 .decode('utf-8') 部分:
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1] + '</prosody></amazon:domain></speak>',
然后我收到一条新的错误消息。
File "main.py", line 50, in index
fw.write(data)
TypeError: write() argument must be str, not bytes
我搜索了 "write() argument must be str, not bytes"。很多网站建议我应该把 .decode('utf-8') 部分。
你能给我一些信息或建议吗?
正如@furas 评论的那样,
open("/tmp/output.mp3", "ba+")
工作完美。谢谢@furas!